Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
OntoLock
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 6
240
0.00% covered (danger)
0.00%
0 / 1
 getLockProperty
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setLock
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 isLocked
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 releaseLock
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 forceReleaseLock
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLockData
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2013 Open Assessment Technologies S.A.
19 *
20 */
21
22namespace oat\tao\model\lock\implementation;
23
24use oat\tao\model\lock\LockSystem;
25use core_kernel_classes_Resource;
26use core_kernel_classes_Property;
27use common_Exception;
28use common_exception_InconsistentData;
29use common_exception_Unauthorized;
30use oat\tao\model\lock\ResourceLockedException;
31use oat\oatbox\service\ConfigurableService;
32use oat\tao\model\TaoOntology;
33
34/**
35 * Implements Lock using a basic property in the ontology storing the lock data
36 *
37 * @note It would be preferably static but we may want to have the polymorphism on lock, but it would be prevented by
38 *       explicit class method static calls. Also if you nevertheless call it statically you may want to avoid the late
39 *       static binding for the getLockProperty
40 */
41class OntoLock extends ConfigurableService implements LockSystem
42{
43    /**
44     *
45     * @return core_kernel_classes_Property
46     */
47    private function getLockProperty()
48    {
49        return new core_kernel_classes_Property(TaoOntology::PROPERTY_LOCK);
50    }
51
52    /**
53     * (non-PHPdoc)
54     * @see \oat\tao\model\lock\LockSystem::setLock()
55     */
56    public function setLock(core_kernel_classes_Resource $resource, $ownerId)
57    {
58        $lock = $this->getLockData($resource);
59        if (is_null($lock)) {
60            $lock = new OntoLockData($resource, $ownerId, microtime(true));
61            $resource->setPropertyValue($this->getLockProperty(), $lock->toJson());
62        } elseif ($lock->getOwnerId() != $ownerId) {
63            throw new ResourceLockedException($lock);
64        }
65    }
66
67    /**
68     * return true is the resource is locked, else otherwise
69     * @return boolean
70     */
71    public function isLocked(core_kernel_classes_Resource $resource)
72    {
73        $values = $resource->getPropertyValues($this->getLockProperty());
74
75        if ((is_array($values)) && (count($values) > 0)) {
76            return true;
77        }
78        return false;
79    }
80
81    /**
82     * release the lock if owned by @user
83     *
84     * @param core_kernel_classes_Resource $resource
85     * @param core_kernel_classes_Resource $user
86     * @throws common_exception_InconsistentData
87     * @throw common_Exception no lock to release
88     */
89    public function releaseLock(core_kernel_classes_Resource $resource, $ownerId)
90    {
91        $lock = $resource->getPropertyValues($this->getLockProperty());
92        if (count($lock) == 0) {
93            return false;
94        } elseif (count($lock) > 1) {
95            throw new common_exception_InconsistentData('Bad data in lock');
96        } else {
97            $lockdata = OntoLockData::getLockData(array_pop($lock));
98            if ($lockdata->getOwnerId() == $ownerId) {
99                $resource->removePropertyValues($this->getLockProperty());
100                return true;
101            } else {
102                throw new common_exception_Unauthorized("The resource is owned by " . $lockdata->getOwnerId());
103            }
104        }
105    }
106
107    /**
108     *  release the lock
109     * @param core_kernel_classes_Resource $resource
110     */
111    public function forceReleaseLock(core_kernel_classes_Resource $resource)
112    {
113        return $resource->removePropertyValues($this->getLockProperty());
114    }
115
116    /**
117     * Return lock details
118     * @param core_kernel_classes_Resource $resource
119     * @throws common_exception_InconsistentData
120     * @return tao_helpers_lock_LockData
121     */
122    public function getLockData(core_kernel_classes_Resource $resource)
123    {
124        $values = $resource->getPropertyValues($this->getLockProperty());
125        if ((is_array($values)) && (count($values) == 1)) {
126            return OntoLockData::getLockData(array_pop($values));
127        } else {
128            return null;
129        }
130    }
131}