Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
NewSqlStorage
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 addRevision
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 saveData
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 getAllRevisions
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getSchema
0.00% covered (danger)
0.00%
0 / 1
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) 2020 (original work) Open Assessment Technologies SA;
19 *
20 */
21
22namespace oat\taoRevision\model\storage;
23
24use core_kernel_classes_Triple as Triple;
25use Doctrine\DBAL\Schema\Schema;
26use Exception;
27use oat\generis\Helper\UuidPrimaryKeyTrait;
28use oat\taoRevision\model\Revision;
29
30class NewSqlStorage extends RdsStorage
31{
32    use UuidPrimaryKeyTrait;
33
34    public const DATA_RESOURCE_ID = 'id';
35
36    /**
37     * @param Revision $revision
38     * @param Triple[] $data
39     *
40     * @return Revision
41     * @throws Exception
42     */
43    public function addRevision(Revision $revision, array $data)
44    {
45        $this->getPersistence()->insert(
46            self::REVISION_TABLE_NAME,
47            [
48                self::REVISION_RESOURCE => $revision->getResourceId(),
49                self::REVISION_VERSION => $revision->getVersion(),
50                self::REVISION_USER => $revision->getAuthorId(),
51                self::REVISION_MESSAGE => $revision->getMessage(),
52                self::REVISION_CREATED => $revision->getDateCreated(),
53            ]
54        );
55
56        if (!empty($data)) {
57            $this->saveData($revision, $data);
58        }
59
60        return $revision;
61    }
62
63    /**
64     * @param Revision $revision
65     * @param array $data
66     * @return bool
67     * @throws Exception
68     */
69    protected function saveData(Revision $revision, array $data)
70    {
71        $dataToSave = [];
72
73        foreach ($data as $triple) {
74            $dataToSave[] = [
75                self::DATA_RESOURCE_ID => (string)$this->getUniquePrimaryKey(),
76                self::DATA_RESOURCE => (string)$revision->getResourceId(),
77                self::DATA_VERSION => (string)$revision->getVersion(),
78                self::DATA_SUBJECT => (string)$triple->subject,
79                self::DATA_PREDICATE => (string)$triple->predicate,
80                self::DATA_OBJECT => (string)$triple->object,
81                self::DATA_LANGUAGE => (string)$triple->lg
82            ];
83        }
84
85        return $this->getPersistence()->insertMultiple(self::DATA_TABLE_NAME, $dataToSave);
86    }
87
88    /**
89     * @param string $resourceId
90     * @return Revision[]
91     */
92    public function getAllRevisions(string $resourceId)
93    {
94        $queryBuilder = $this->getQueryBuilder()
95            ->select('*')
96            ->from(self::REVISION_TABLE_NAME)
97            ->where(sprintf(' `%s` = ? ', self::REVISION_RESOURCE))
98            ->setParameters([$resourceId]);
99
100        $variables = $this->getPersistence()
101            ->query($queryBuilder->getSQL())
102            ->fetchAll();
103
104        return $this->buildRevisionCollection($variables);
105    }
106
107    /**
108     * @inheritDoc
109     */
110    public function getSchema(Schema $schema)
111    {
112        return $this->getServiceLocator()->get(NewSqlSchema::class)->getSchema($schema);
113    }
114}