Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.40% covered (success)
97.40%
75 / 77
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Source
97.40% covered (success)
97.40%
75 / 77
75.00% covered (warning)
75.00%
6 / 8
26
0.00% covered (danger)
0.00%
0 / 1
 readEntity
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
7
 readList
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 readResolvedReferences
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
 writeEntity
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
4.00
 writeInsertRelations
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 writeDeleteBySource
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 writeDeleteRelations
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 removeCache
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
1<?php
2
3namespace BO\Zmsdb;
4
5use BO\Zmsdb\Application as App;
6use BO\Zmsentities\Collection\ScopeList;
7use BO\Zmsentities\Source as Entity;
8use BO\Zmsentities\Collection\SourceList as Collection;
9use BO\Zmsentities\Collection\ProviderList;
10use BO\Zmsentities\Collection\RequestList;
11use BO\Zmsentities\Collection\RequestRelationList;
12
13/**
14 *
15 * @SuppressWarnings(Coupling)
16 *
17 */
18class Source extends Base
19{
20    /**
21     * Fetch source from db
22     *
23     * @return \BO\Zmsentities\Source
24     */
25    public function readEntity($sourceName, $resolveReferences = 0, $disableCache = false)
26    {
27        $cacheKey = "source-$sourceName-$resolveReferences";
28
29        if (!$disableCache && App::$cache && App::$cache->has($cacheKey)) {
30            $entity = App::$cache->get($cacheKey);
31        }
32
33        if (empty($entity)) {
34            $query = new Query\Source(Query\Base::SELECT);
35            $query
36                ->addEntityMapping()
37                ->addResolvedReferences($resolveReferences)
38                ->addConditionSource($sourceName);
39            $entity = $this->fetchOne($query, new Entity());
40            if (! $entity->hasId()) {
41                return null;
42            }
43
44            if (App::$cache) {
45                App::$cache->set($cacheKey, $entity);
46            }
47        }
48
49        return $this->readResolvedReferences($entity, $resolveReferences, $disableCache);
50    }
51
52    /**
53     * read a list of sources
54     *
55     * @return \BO\Zmsentities\Collection\SourceList
56     */
57    public function readList($resolveReferences = 0)
58    {
59        $collection = new Collection();
60        $query = new Query\Source(Query\Base::SELECT);
61        $query->addEntityMapping()
62            ->addResolvedReferences($resolveReferences);
63        $result = $this->fetchList($query, new Entity());
64        if (count($result)) {
65            foreach ($result as $entity) {
66                if ($entity instanceof Entity) {
67                    $entity = $this->readResolvedReferences($entity, $resolveReferences);
68                    $collection->addEntity($entity);
69                }
70            }
71        }
72        return $collection;
73    }
74
75    /**
76     * resolve entity references
77     *
78     * @return \BO\Zmsentities\Source
79     */
80    public function readResolvedReferences(
81        \BO\Zmsentities\Schema\Entity $entity,
82        $resolveReferences,
83        $disableCache = false
84    ) {
85        if (0 < $resolveReferences) {
86            $entity['providers'] = (new ProviderList())
87                ->addList((new Provider())->readListBySource($entity->source, $resolveReferences - 1));
88            $entity['requests'] = (new RequestList())
89                ->addList((new Request())->readListBySource(
90                    $entity->source,
91                    $resolveReferences - 1,
92                    $disableCache
93                ));
94            $entity['requestrelation'] = (new RequestRelationList())
95                ->addList((new RequestRelation())->readListBySource($entity->source));
96            $entity['scopes'] = (new ScopeList())
97                ->addList((new Scope())->readList($disableCache));
98        }
99
100        return $entity;
101    }
102
103    /**
104     * write or update a source
105     *
106     * @return \BO\Zmsentities\Source
107     */
108    public function writeEntity(Entity $entity, $resolveReferences = 0)
109    {
110        if (! $entity->isCompleteAndEditable()) {
111            throw new Exception\Source\SourceInvalidInput();
112        }
113        $this->writeDeleteBySource($entity->getSource());
114        $query = new Query\Source(Query\Base::INSERT);
115        $query->addValues(
116            array(
117                'source' => $entity->getSource(),
118                'label' => $entity->getLabel(),
119                'editable' => ($entity->isEditable()) ? 1 : 0,
120                'contact__name' => $entity->contact['name'],
121                'contact__email' => $entity->contact['email']
122            )
123        );
124        if ($this->writeItem($query)) {
125            $this->writeInsertRelations($entity);
126        }
127
128        $this->removeCache($entity->getSource());
129
130        return $this->readEntity($entity->getSource(), $resolveReferences, true);
131    }
132
133    /**
134     * delete provider and request relations of source
135     *
136     */
137    public function writeInsertRelations(\BO\Zmsentities\Source $entity)
138    {
139        (new Provider())->writeListBySource($entity);
140        (new Request())->writeListBySource($entity);
141        (new RequestRelation())->writeListBySource($entity);
142    }
143
144    /**
145     * delete by sourcename
146     *
147     * @return \BO\Zmsentities\Source
148     */
149    public function writeDeleteBySource($sourceName)
150    {
151        $entity = $this->readEntity($sourceName);
152        $query = new Query\Source(Query\Base::DELETE);
153        $query->addConditionSource($sourceName);
154        $this->writeDeleteRelations($sourceName);
155
156        $this->removeCache($sourceName);
157
158        return ($this->deleteItem($query)) ? $entity : null;
159    }
160
161    /**
162     * delete provider and request relations of source
163     *
164     */
165    public function writeDeleteRelations($sourceName)
166    {
167        (new Provider())->writeDeleteListBySource($sourceName);
168        (new Request())->writeDeleteListBySource($sourceName);
169        (new RequestRelation())->writeDeleteListBySource($sourceName);
170    }
171
172    public function removeCache($sourceName)
173    {
174        if (!App::$cache) {
175            return;
176        }
177
178        if (App::$cache->has("source-$sourceName-0")) {
179            App::$cache->delete("source-$sourceName-0");
180        }
181
182        if (App::$cache->has("source-$sourceName-1")) {
183            App::$cache->delete("source-$sourceName-1");
184        }
185
186        if (App::$cache->has("source-$sourceName-2")) {
187            App::$cache->delete("source-$sourceName-2");
188        }
189    }
190}