Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
54.35% covered (warning)
54.35%
25 / 46
68.75% covered (warning)
68.75%
11 / 16
CRAP
0.00% covered (danger)
0.00%
0 / 1
core_kernel_persistence_smoothsql_SmoothModel
54.35% covered (warning)
54.35%
25 / 46
68.75% covered (warning)
68.75%
11 / 16
62.96
0.00% covered (danger)
0.00%
0 / 1
 getResource
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getClass
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getProperty
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 isWritable
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getPersistence
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getCache
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRdfInterface
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRdfsInterface
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSearchInterface
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getNewTripleModelId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getReadableModels
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getWritableModels
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addReadableModel
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getReadableModelIds
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getUpdatableModelIds
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 provideSchema
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
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) (original work) 2015 Open Assessment Technologies SA
19 *
20 */
21
22use oat\generis\model\data\ModelManager;
23use oat\generis\model\data\Ontology;
24use oat\generis\model\kernel\persistence\Cacheable;
25use oat\generis\model\kernel\persistence\smoothsql\install\SmoothRdsModel;
26use oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService;
27use oat\generis\persistence\sql\SchemaCollection;
28use oat\generis\persistence\sql\SchemaProviderInterface;
29use oat\oatbox\cache\PropertyCache;
30use oat\oatbox\cache\SimpleCache;
31use oat\oatbox\service\ConfigurableService;
32
33/**
34 * transitory model for the smooth sql implementation
35 *
36 * @author joel bout <joel@taotesting.com>
37 * @package generis
38 */
39class core_kernel_persistence_smoothsql_SmoothModel extends ConfigurableService implements
40    Ontology,
41    SchemaProviderInterface,
42    Cacheable
43{
44    public const OPTION_PERSISTENCE = 'persistence';
45    public const OPTION_READABLE_MODELS = 'readable';
46    public const OPTION_WRITEABLE_MODELS = 'writeable';
47    public const OPTION_NEW_TRIPLE_MODEL = 'addTo';
48    public const OPTION_SEARCH_SERVICE = 'search';
49
50    public const DEFAULT_WRITABLE_MODEL = 1;
51    public const DEFAULT_READ_ONLY_MODEL = 2;
52
53    /**
54     * Persistence to use for the smoothmodel
55     *
56     * @var common_persistence_SqlPersistence
57     */
58    private $persistence;
59
60    public function getResource($uri)
61    {
62        $resource = new \core_kernel_classes_Resource($uri);
63        $resource->setModel($this);
64        return $resource;
65    }
66
67    public function getClass($uri)
68    {
69        $class = new \core_kernel_classes_Class($uri);
70        $class->setModel($this);
71        return $class;
72    }
73
74    public function getProperty($uri)
75    {
76        $property = new \core_kernel_classes_Property($uri);
77        $property->setModel($this);
78        return $property;
79    }
80
81    public function isWritable(core_kernel_classes_Resource $resource): bool
82    {
83        $writableModels = $this->getWritableModels();
84
85        /** @var core_kernel_classes_Triple $triple */
86        foreach ($resource->getRdfTriples() as $triple) {
87            if (!in_array((int)$triple->modelid, $writableModels, true)) {
88                return false;
89            }
90        }
91
92        return true;
93    }
94
95    /**
96     * @return common_persistence_SqlPersistence
97     */
98    public function getPersistence()
99    {
100        if (is_null($this->persistence)) {
101            $this->persistence = $this->getServiceLocator()
102                ->get(common_persistence_Manager::SERVICE_ID)
103                ->getPersistenceById($this->getOption(self::OPTION_PERSISTENCE));
104        }
105        return $this->persistence;
106    }
107
108    public function getCache(): SimpleCache
109    {
110        return $this->getServiceLocator()->get(PropertyCache::SERVICE_ID);
111    }
112
113    /**
114     * (non-PHPdoc)
115     * @see \oat\generis\model\data\Model::getRdfInterface()
116     */
117    public function getRdfInterface()
118    {
119        return new core_kernel_persistence_smoothsql_SmoothRdf($this);
120    }
121
122    /**
123     * (non-PHPdoc)
124     * @see \oat\generis\model\data\Model::getRdfsInterface()
125     */
126    public function getRdfsInterface()
127    {
128        return new core_kernel_persistence_smoothsql_SmoothRdfs($this);
129    }
130
131    /**
132     * @return ComplexSearchService
133     */
134    public function getSearchInterface()
135    {
136        $search = $this->getServiceLocator()->get($this->getOption(self::OPTION_SEARCH_SERVICE));
137        $search->setModel($this);
138        return $search;
139    }
140
141    // Manage the sudmodels of the smooth mode
142
143    /**
144     * Returns the id of the model to add to
145     *
146     * @return string
147     */
148    public function getNewTripleModelId()
149    {
150        return $this->getOption(self::OPTION_NEW_TRIPLE_MODEL);
151    }
152
153    public function getReadableModels()
154    {
155        return $this->getOption(self::OPTION_READABLE_MODELS);
156    }
157
158    public function getWritableModels()
159    {
160        return $this->getOption(self::OPTION_WRITEABLE_MODELS);
161    }
162
163    //
164    // Deprecated functions
165    //
166
167    /**
168     * Defines a model as readable
169     *
170     * @param string $id
171     */
172    public function addReadableModel($id)
173    {
174
175        common_Logger::i('ADDING MODEL ' . $id);
176
177        $readables = $this->getOption(self::OPTION_READABLE_MODELS);
178        $this->setOption(self::OPTION_READABLE_MODELS, array_unique(array_merge($readables, [$id])));
179
180        // update in persistence
181        ModelManager::setModel($this);
182    }
183
184    /**
185     * Returns the submodel ids that are readable
186     *
187     * @deprecated
188     * @return array()
189     */
190    public static function getReadableModelIds()
191    {
192        $model = ModelManager::getModel();
193        if (!$model instanceof self) {
194            throw new common_exception_Error(
195                __FUNCTION__ . ' called on ' . get_class($model) . ' model implementation'
196            );
197        }
198        return $model->getReadableModels();
199    }
200
201    /**
202     * Returns the submodel ids that are updatable
203     *
204     * @deprecated
205     * @return array()
206     */
207    public static function getUpdatableModelIds()
208    {
209        $model = ModelManager::getModel();
210        if (!$model instanceof self) {
211            throw new common_exception_Error(
212                __FUNCTION__ . ' called on ' . get_class($model) . ' model implementation'
213            );
214        }
215        return $model->getWritableModels();
216    }
217
218    public function provideSchema(SchemaCollection $schemaCollection)
219    {
220        $schema = $schemaCollection->getSchema($this->getOption(self::OPTION_PERSISTENCE));
221        SmoothRdsModel::addSmoothTables($schema);
222    }
223}