Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateDataAccessControlInIndex
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 __invoke
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
4
 getParentClasses
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
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) 2018 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\tao\model\search\tasks;
24
25use common_exception_MissingParameter;
26use common_report_Report as Report;
27use core_kernel_classes_Class;
28use core_kernel_classes_Resource;
29use oat\generis\model\OntologyAwareTrait;
30use oat\oatbox\action\Action;
31use oat\oatbox\log\LoggerAwareTrait;
32use oat\tao\model\search\index\IndexUpdaterInterface;
33use oat\tao\model\taskQueue\Task\TaskAwareInterface;
34use oat\tao\model\taskQueue\Task\TaskAwareTrait;
35use Throwable;
36use Zend\ServiceManager\ServiceLocatorAwareInterface;
37use Zend\ServiceManager\ServiceLocatorAwareTrait;
38
39class UpdateDataAccessControlInIndex implements Action, ServiceLocatorAwareInterface, TaskAwareInterface
40{
41    use ServiceLocatorAwareTrait;
42    use OntologyAwareTrait;
43    use TaskAwareTrait;
44    use LoggerAwareTrait;
45    use IndexTrait {
46        getParentClasses as getParentClassesOfClass;
47    }
48
49    public const READ_ACCESS_PROPERTY = 'read_access';
50
51    /**
52     * @param $params
53     *
54     * @return Report
55     *
56     * @throws common_exception_MissingParameter
57     */
58    public function __invoke($params): Report
59    {
60        if (!is_array($params) || count($params) < 2) {
61            throw new common_exception_MissingParameter();
62        }
63
64        [$resourceUri, $newPermissions] = $params;
65
66        $resource = $this->getResource($resourceUri);
67
68        $parentClasses = $this->getParentClasses($resource);
69
70        /** @var IndexUpdaterInterface $indexUpdater */
71        $indexUpdater = $this->getServiceLocator()->get(IndexUpdaterInterface::SERVICE_ID);
72
73        $type = Report::TYPE_SUCCESS;
74        $message = 'Documents in index were successfully updated.';
75        $logMessage = 'Data Access Control were being updated by ' . static::class;
76
77        try {
78            $indexUpdater->updatePropertyValue(
79                $resourceUri,
80                $parentClasses,
81                self::READ_ACCESS_PROPERTY,
82                $newPermissions
83            );
84        } catch (Throwable $exception) {
85            $type = Report::TYPE_ERROR;
86            $message = 'Failed during update search index';
87            $logMessage = 'Data Access Control failure: (' . get_class($exception) . ') ' . $exception->getMessage();
88        }
89
90        $this->logInfo($logMessage);
91        return new Report($type, $message);
92    }
93
94    /**
95     * @param core_kernel_classes_Resource $resource
96     *
97     * @return array
98     */
99    private function getParentClasses(core_kernel_classes_Resource $resource): array
100    {
101        $parentClasses = [];
102
103        if ($resource->isClass()) {
104            /** @noinspection PhpParamsInspection */
105            $parentClasses = $this->getParentClassesOfClass(
106                $this->getClass($resource->getUri())
107            );
108
109            return $parentClasses;
110        }
111
112        /** @var core_kernel_classes_Class $type */
113        foreach ($resource->getTypes() as $type) {
114            $parentClasses = array_merge($parentClasses, [$type->getUri()], $this->getParentClassesOfClass($type));
115        }
116        return $parentClasses;
117    }
118}