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