GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( a41792...2c7b14 )
by Mario
02:15
created

InformationCollectionService   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 300
Duplicated Lines 9.67 %

Coupling/Cohesion

Components 1
Dependencies 25

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 25
dl 29
loc 300
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A createCollection() 0 38 5
A getObjectsWithCollectionsCount() 0 6 1
A getObjectsWithCollections() 0 33 2
A getCollectionsCount() 0 6 1
A getCollections() 0 18 2
A searchCount() 0 14 1
A search() 0 21 2
A getCollection() 0 4 1
A deleteCollectionFields() 0 13 1
A deleteCollections() 15 15 2
A deleteCollectionByContent() 14 14 2
A getUser() 0 9 2
A loadCollection() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Netgen\InformationCollection\Core\Service;
6
7
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
8
use eZ\Publish\API\Repository\Repository;
9
use eZ\Publish\API\Repository\Values\User\UserReference;
10
use Netgen\InformationCollection\API\Exception\PersistingFailedException;
11
use Netgen\InformationCollection\API\Exception\StoringAttributeFailedException;
12
use Netgen\InformationCollection\API\Exception\StoringCollectionFailedException;
13
use Netgen\InformationCollection\API\Service\InformationCollection;
14
use Netgen\InformationCollection\API\Value\Collection;
15
use Netgen\InformationCollection\API\Value\CollectionCount;
16
use Netgen\InformationCollection\API\Value\Collections;
17
use Netgen\InformationCollection\API\Value\ContentsWithCollections;
18
use Netgen\InformationCollection\API\Value\Filter\CollectionFields;
19
use Netgen\InformationCollection\API\Value\Filter\CollectionId;
20
use Netgen\InformationCollection\API\Value\Filter\Collections as FilterCollections;
21
use Netgen\InformationCollection\API\Value\Filter\ContentId;
22
use Netgen\InformationCollection\API\Value\Filter\Contents;
23
use Netgen\InformationCollection\API\Value\Filter\Query;
24
use Netgen\InformationCollection\API\Value\Filter\SearchCountQuery;
25
use Netgen\InformationCollection\API\Value\Filter\SearchQuery;
26
use Netgen\InformationCollection\API\Value\InformationCollectionStruct;
27
use Netgen\InformationCollection\API\Value\ObjectCount;
28
use Netgen\InformationCollection\API\Value\SearchCount;
29
use Netgen\InformationCollection\Core\Factory\FieldDataFactory;
30
use Netgen\InformationCollection\Core\Mapper\DomainObjectMapper;
31
use Netgen\InformationCollection\Core\Persistence\Gateway\DoctrineDatabase;
32
use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository;
33
use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository;
34
35
class InformationCollectionService implements InformationCollection
36
{
37
    /**
38
     * @var \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository
39
     */
40
    protected $ezInfoCollectionRepository;
41
42
    /**
43
     * @var \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository
44
     */
45
    protected $ezInfoCollectionAttributeRepository;
46
47
    /**
48
     * @var \eZ\Publish\API\Repository\Repository
49
     */
50
    protected $repository;
51
52
    /**
53
     * @var \Netgen\InformationCollection\Core\Persistence\Gateway\DoctrineDatabase
54
     */
55
    protected $gateway;
56
57
    /**
58
     * @var \Netgen\InformationCollection\Core\Factory\FieldDataFactory
59
     */
60
    protected $fieldsFactory;
61
62
    /**
63
     * @var \Netgen\InformationCollection\Core\Mapper\DomainObjectMapper
64
     */
65
    protected $objectMapper;
66
67
    /**
68
     * InformationCollectionService constructor.
69
     *
70
     * @param \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository $ezInfoCollectionRepository
71
     * @param \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository $ezInfoCollectionAttributeRepository
72
     * @param \eZ\Publish\API\Repository\Repository $repository
73
     * @param \Netgen\InformationCollection\Core\Persistence\Gateway\DoctrineDatabase $gateway
74
     */
75
    public function __construct(
76
        EzInfoCollectionRepository $ezInfoCollectionRepository,
77
        EzInfoCollectionAttributeRepository $ezInfoCollectionAttributeRepository,
78
        Repository $repository,
79
        DoctrineDatabase $gateway,
80
        FieldDataFactory $factory,
81
        DomainObjectMapper $objectMapper
82
    ) {
83
        $this->ezInfoCollectionRepository = $ezInfoCollectionRepository;
84
        $this->ezInfoCollectionAttributeRepository = $ezInfoCollectionAttributeRepository;
85
        $this->repository = $repository;
86
        $this->gateway = $gateway;
87
        $this->fieldsFactory = $factory;
88
        $this->objectMapper = $objectMapper;
89
    }
90
91
    public function createCollection(InformationCollectionStruct $struct): void
92
    {
93
        $contentType = $struct->getContentType();
94
        $content = $struct->getContent();
95
96
        $userReference = $this->repository
97
            ->getPermissionResolver()
98
            ->getCurrentUserReference();
99
100
        $user = $this->getUser(
101
            $userReference->getUserId()
102
        );
103
104
        $ezInfo = $this->ezInfoCollectionRepository
105
            ->createNewFromValues($content, $user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->getUser($userReference->getUserId()) on line 100 can be null; however, Netgen\InformationCollec...::createNewFromValues() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
106
107
        try {
108
            $this->ezInfoCollectionRepository->save($ezInfo);
109
        } catch (StoringCollectionFailedException $e) {
110
            throw new PersistingFailedException('collection', $e->getMessage());
111
        }
112
113
        foreach ($struct->getFieldsData() as $fieldDefIdentifier => $value) {
114
            if ($value === null) {
115
                continue;
116
            }
117
118
            $value = $this->fieldsFactory->getLegacyValue($value->value, $contentType->getFieldDefinition($fieldDefIdentifier));
119
            $ezInfoAttribute = $this->ezInfoCollectionAttributeRepository
120
                ->createNewFromValues($content, $ezInfo, $value, $fieldDefIdentifier);
121
122
            try {
123
                $this->ezInfoCollectionAttributeRepository->save($ezInfoAttribute);
124
            } catch (StoringAttributeFailedException $e) {
125
                throw new PersistingFailedException('attribute', $e->getMessage());
126
            }
127
        }
128
    }
129
130
    public function getObjectsWithCollectionsCount(): ObjectCount
131
    {
132
        return new ObjectCount(
133
            $this->gateway->getContentsWithCollectionsCount()
134
        );
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function getObjectsWithCollections(Query $query): ContentsWithCollections
141
    {
142
        $objects = $this->gateway->getObjectsWithCollections($query->getLimit(), $query->getOffset());
143
144
        $contents = [];
145
        foreach ($objects as $object) {
146
            $contentId = (int) $object['content_id'];
147
148
            $firstCollection = $this->ezInfoCollectionRepository->findOneBy(
149
                [
150
                    'contentObjectId' => $contentId,
151
                ],
152
                [
153
                    'created' => 'ASC',
154
                ]
155
            );
156
157
            $lastCollection = $this->ezInfoCollectionRepository->findOneBy(
158
                [
159
                    'contentObjectId' => $contentId,
160
                ],
161
                [
162
                    'created' => 'DESC',
163
                ]
164
            );
165
166
            $childCount = $this->ezInfoCollectionRepository->getChildrenCount($contentId);
167
168
            $contents[] = $this->objectMapper->mapContent($object, $firstCollection, $lastCollection, $childCount);
0 ignored issues
show
Documentation introduced by
$firstCollection is of type object|null, but the function expects a object<Netgen\Informatio...ntity\EzInfoCollection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$lastCollection is of type object|null, but the function expects a object<Netgen\Informatio...ntity\EzInfoCollection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
169
        }
170
171
        return new ContentsWithCollections($contents, count($contents));
172
    }
173
174
    public function getCollectionsCount(ContentId $contentId): CollectionCount
175
    {
176
        return new CollectionCount(
177
            $this->ezInfoCollectionRepository->getChildrenCount($contentId->getContentId())
178
        );
179
    }
180
181
    public function getCollections(ContentId $contentId): Collections
182
    {
183
        $collections = $this->ezInfoCollectionRepository->findBy(
184
            [
185
                'contentObjectId' => $contentId->getContentId(),
186
            ],
187
            [],
188
            $contentId->getLimit(),
189
            $contentId->getOffset()
190
        );
191
192
        $objects = [];
193
        foreach ($collections as $collection) {
194
            $objects[] = $this->loadCollection($collection->getId());
195
        }
196
197
        return new Collections($objects, count($objects));
198
    }
199
200
    public function searchCount(SearchCountQuery $query): SearchCount
201
    {
202
        $collections = $this->ezInfoCollectionAttributeRepository
203
            ->search($query->getContentId(), $query->getSearchText());
204
205
        // needs rewrite
206
        $collections = $this->ezInfoCollectionRepository->findBy(
207
            [
208
                'id' => $collections,
209
            ]
210
        );
211
212
        return new SearchCount(count($collections));
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218
    public function search(SearchQuery $query): Collections
219
    {
220
        $collections = $this->ezInfoCollectionAttributeRepository
221
            ->search($query->getContentId(), $query->getSearchText());
222
223
        $collections = $this->ezInfoCollectionRepository->findBy(
224
            [
225
                'id' => $collections,
226
            ],
227
            [],
228
            $query->getLimit(),
229
            $query->getOffset()
230
        );
231
232
        $objects = [];
233
        foreach ($collections as $collection) {
234
            $objects[] = $this->loadCollection($collection->getId());
235
        }
236
237
        return new Collections($objects, count($objects));
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243
    public function getCollection(CollectionId $collectionId): Collection
244
    {
245
        return $this->loadCollection($collectionId->getCollectionId());
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251
    public function deleteCollectionFields(CollectionFields $collectionFields): void
252
    {
253
        $attributes = $this->ezInfoCollectionAttributeRepository
254
            ->findBy(
255
                [
256
                    'contentObjectId' => $collectionFields->getContentId(),
257
                    'informationCollectionId' => $collectionFields->getCollectionId(),
258
                    'contentClassAttributeId' => $collectionFields->getFields(),
259
                ]
260
            );
261
262
        $this->ezInfoCollectionAttributeRepository->remove($attributes);
263
    }
264
265
    /**
266
     * {@inheritdoc}
267
     */
268 View Code Duplication
    public function deleteCollections(FilterCollections $collections): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
269
    {
270
        $collections = $this->ezInfoCollectionRepository
271
            ->findBy([
272
                'contentObjectId' => $collections->getContentId(),
273
                'id' => $collections->getCollectionIds(),
274
            ]);
275
276
        foreach ($collections as $collection) {
277
            $attributes = $this->ezInfoCollectionAttributeRepository->findBy(['informationCollectionId' => $collection->getId()]);
278
            $this->ezInfoCollectionAttributeRepository->remove($attributes);
279
        }
280
281
        $this->ezInfoCollectionRepository->remove($collections);
282
    }
283
284
    /**
285
     * {@inheritdoc}
286
     */
287 View Code Duplication
    public function deleteCollectionByContent(Contents $contents): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
288
    {
289
        $collections = $this->ezInfoCollectionRepository
290
            ->findBy([
291
                'contentObjectId' => $contents->getContents(),
0 ignored issues
show
Bug introduced by
The method getContents() does not exist on Netgen\InformationCollec...I\Value\Filter\Contents. Did you maybe mean getContentIds()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
292
            ]);
293
294
        foreach ($collections as $collection) {
295
            $attributes = $this->ezInfoCollectionAttributeRepository->findBy(['informationCollectionId' => $collection->getId()]);
296
            $this->ezInfoCollectionAttributeRepository->remove($attributes);
297
        }
298
299
        $this->ezInfoCollectionRepository->remove($collections);
300
    }
301
302
    /**
303
     * @param UserReference $userReference
0 ignored issues
show
Bug introduced by
There is no parameter named $userReference. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
304
     * @param mixed $userId
305
     *
306
     * @return \eZ\Publish\API\Repository\Values\User\User
307
     */
308
    protected function getUser($userId)
309
    {
310
        try {
311
            return $this->repository
312
                ->getUserService()
313
                ->loadUser($userId);
314
        } catch (NotFoundException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
315
        }
316
    }
317
318
    /**
319
     * @param int $collectionId
320
     *
321
     * @return \Netgen\InformationCollection\API\Value\Collection
322
     */
323
    protected function loadCollection($collectionId)
324
    {
325
        $collection = $this->ezInfoCollectionRepository->findOneBy(['id' => $collectionId]);
326
        $attributes = $this->ezInfoCollectionAttributeRepository->findBy(
327
            [
328
                'informationCollectionId' => $collectionId,
329
            ]
330
        );
331
332
        return $this->objectMapper->mapCollection($collection, $attributes);
0 ignored issues
show
Documentation introduced by
$collection is of type object|null, but the function expects a object<Netgen\Informatio...ntity\EzInfoCollection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
333
    }
334
}
335