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 ( c7f4e3...a41792 )
by Mario
18:09
created

InformationCollectionService::deleteCollections()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\ContentId;
21
use Netgen\InformationCollection\API\Value\Filter\Contents;
22
use Netgen\InformationCollection\API\Value\Filter\Query;
23
use Netgen\InformationCollection\API\Value\Filter\SearchCountQuery;
24
use Netgen\InformationCollection\API\Value\Filter\SearchQuery;
25
use Netgen\InformationCollection\API\Value\Filter\Collections as FilterCollections;
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\EzInfoCollectionRepository;
33
use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository;
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 $factory
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
114
        foreach ($struct->getFieldsData() as $fieldDefIdentifier => $value) {
115
            if ($value === null) {
116
                continue;
117
            }
118
119
            $value = $this->fieldsFactory->getLegacyValue($value->value, $contentType->getFieldDefinition($fieldDefIdentifier));
120
            $ezInfoAttribute = $this->ezInfoCollectionAttributeRepository
121
                ->createNewFromValues($content, $ezInfo, $value, $fieldDefIdentifier);
122
123
            try {
124
                $this->ezInfoCollectionAttributeRepository->save($ezInfoAttribute);
125
            } catch (StoringAttributeFailedException $e) {
126
                throw new PersistingFailedException('attribute', $e->getMessage());
127
            }
128
        }
129
    }
130
131
132
    public function getObjectsWithCollectionsCount(): ObjectCount
133
    {
134
        return new ObjectCount(
135
            $this->gateway->getContentsWithCollectionsCount()
136
        );
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function getObjectsWithCollections(Query $query): ContentsWithCollections
143
    {
144
        $objects = $this->gateway->getObjectsWithCollections($query->getLimit(), $query->getOffset());
145
146
        $contents = [];
147
        foreach ($objects as $object) {
148
            $contentId = (int) $object['content_id'];
149
150
            $firstCollection = $this->ezInfoCollectionRepository->findOneBy(
151
                [
152
                    'contentObjectId' => $contentId,
153
                ],
154
                [
155
                    'created' => 'ASC',
156
                ]
157
            );
158
159
            $lastCollection = $this->ezInfoCollectionRepository->findOneBy(
160
                [
161
                    'contentObjectId' => $contentId,
162
                ],
163
                [
164
                    'created' => 'DESC',
165
                ]
166
            );
167
168
            $childCount = $this->ezInfoCollectionRepository->getChildrenCount($contentId);
169
170
            $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...
171
        }
172
173
        return new ContentsWithCollections($contents, count($contents));
174
    }
175
176
    public function getCollectionsCount(ContentId $contentId): CollectionCount
177
    {
178
        return new CollectionCount(
179
            $this->ezInfoCollectionRepository->getChildrenCount($contentId->getContentId())
180
        );
181
    }
182
183
    public function getCollections(ContentId $contentId): Collections
184
    {
185
        $collections = $this->ezInfoCollectionRepository->findBy(
186
            [
187
                'contentObjectId' => $contentId->getContentId(),
188
            ],
189
            [],
190
            $contentId->getLimit(),
191
            $contentId->getOffset()
192
        );
193
194
        $objects = [];
195
        foreach ($collections as $collection) {
196
            $objects[] = $this->loadCollection($collection->getId());
197
        }
198
199
        return new Collections($objects, count($objects));
200
    }
201
202
    public function searchCount(SearchCountQuery $query): SearchCount
203
    {
204
        $collections = $this->ezInfoCollectionAttributeRepository
205
            ->search($query->getContentId(), $query->getSearchText());
206
207
        // needs rewrite
208
        $collections = $this->ezInfoCollectionRepository->findBy(
209
            [
210
                'id' => $collections,
211
            ]
212
        );
213
214
        return new SearchCount(count($collections));
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function search(SearchQuery $query): Collections
221
    {
222
        $collections = $this->ezInfoCollectionAttributeRepository
223
            ->search($query->getContentId(), $query->getSearchText());
224
225
        $collections = $this->ezInfoCollectionRepository->findBy(
226
            [
227
                'id' => $collections,
228
            ],
229
            [],
230
            $query->getLimit(),
231
            $query->getOffset()
232
        );
233
234
        $objects = [];
235
        foreach ($collections as $collection) {
236
            $objects[] = $this->loadCollection($collection->getId());
237
        }
238
239
        return new Collections($objects, count($objects));
240
    }
241
242
    /**
243
     * {@inheritdoc}
244
     */
245
    public function getCollection(CollectionId $collectionId): Collection
246
    {
247
        return $this->loadCollection($collectionId->getCollectionId());
248
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253
    public function deleteCollectionFields(CollectionFields $collectionFields): void
254
    {
255
        $attributes = $this->ezInfoCollectionAttributeRepository
256
            ->findBy(
257
                [
258
                    'contentObjectId' => $collectionFields->getContentId(),
259
                    'informationCollectionId' => $collectionFields->getCollectionId(),
260
                    'contentClassAttributeId' => $collectionFields->getFields(),
261
                ]);
262
263
        $this->ezInfoCollectionAttributeRepository->remove($attributes);
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269 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...
270
    {
271
        $collections = $this->ezInfoCollectionRepository
272
            ->findBy([
273
                'contentObjectId' => $collections->getContentId(),
274
                'id' => $collections->getCollectionIds(),
275
            ]);
276
277
        foreach ($collections as $collection) {
278
            $attributes = $this->ezInfoCollectionAttributeRepository->findBy(['informationCollectionId' => $collection->getId()]);
279
            $this->ezInfoCollectionAttributeRepository->remove($attributes);
280
        }
281
282
        $this->ezInfoCollectionRepository->remove($collections);
283
    }
284
285
    /**
286
     * {@inheritdoc}
287
     */
288 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...
289
    {
290
        $collections = $this->ezInfoCollectionRepository
291
            ->findBy([
292
                '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...
293
            ]);
294
295
        foreach ($collections as $collection) {
296
            $attributes = $this->ezInfoCollectionAttributeRepository->findBy(['informationCollectionId' => $collection->getId()]);
297
            $this->ezInfoCollectionAttributeRepository->remove($attributes);
298
        }
299
300
        $this->ezInfoCollectionRepository->remove($collections);
301
    }
302
303
    /**
304
     * @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...
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
315
        } catch (NotFoundException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
316
317
        }
318
319
    }
320
321
    /**
322
     * @param int $collectionId
323
     *
324
     * @return \Netgen\InformationCollection\API\Value\Collection
325
     */
326
    protected function loadCollection($collectionId)
327
    {
328
        $collection = $this->ezInfoCollectionRepository->findOneBy(['id' => $collectionId]);
329
        $attributes = $this->ezInfoCollectionAttributeRepository->findBy(
330
            [
331
                'informationCollectionId' => $collectionId,
332
            ]
333
        );
334
335
        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...
336
    }
337
}
338