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 ( 36acbf )
by Mario
11:02
created

DomainObjectMapper::mapAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Netgen\InformationCollection\Core\Mapper;
6
7
use DateTimeImmutable;
8
use DateTimeInterface;
9
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
10
use eZ\Publish\API\Repository\Repository;
11
use eZ\Publish\API\Repository\Values\Content\Content as APIContent;
12
use eZ\Publish\API\Repository\Values\Content\Field;
13
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
14
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCollection;
15
use eZ\Publish\API\Repository\Values\User\User;
16
use eZ\Publish\Core\Repository\Values\ContentType\ContentType as CoreContentType;
17
use Netgen\InformationCollection\API\Value\Attribute;
18
use Netgen\InformationCollection\API\Value\AttributeValue;
19
use Netgen\InformationCollection\API\Value\Collection;
20
use Netgen\InformationCollection\API\Value\Content;
21
use Netgen\InformationCollection\Doctrine\Entity\EzInfoCollection;
22
use Netgen\InformationCollection\Doctrine\Entity\EzInfoCollectionAttribute;
23
24
final class DomainObjectMapper
25
{
26
    /**
27
     * @var \eZ\Publish\API\Repository\Repository
28
     */
29
    private $repository;
30
31
    /**
32
     * @var \eZ\Publish\API\Repository\ContentService
33
     */
34
    private $contentService;
35
36
    /**
37
     * @var \eZ\Publish\API\Repository\ContentTypeService
38
     */
39
    private $contentTypeService;
40
41
    /**
42
     * @var \eZ\Publish\API\Repository\UserService
43
     */
44
    private $userService;
45
46
    public function __construct(Repository $repository)
47
    {
48
        $this->repository = $repository;
49
        $this->contentService = $repository->getContentService();
50
        $this->contentTypeService = $repository->getContentTypeService();
51
    }
52
53
    public function mapContent(array $data, EzInfoCollection $first, EzInfoCollection $last, int $childCount): Content
54
    {
55
        $content = $this->contentService->loadContent((int) $data['content_id']);
56
        $contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId);
57
        $hasLocation = empty($object['main_node_id']) ? false : true;
0 ignored issues
show
Bug introduced by
The variable $object seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
58
59
        return new Content(
60
            $content,
61
            $contentType,
62
            $this->mapCollection($first, []),
63
            $this->mapCollection($last, []),
64
            $childCount,
65
            $hasLocation
66
        );
67
    }
68
69
    public function mapCollection(EzInfoCollection $collection, array $attributes): Collection
70
    {
71
        $content = $this->contentService->loadContent($collection->getContentObjectId());
72
        /** @var CoreContentType $contentType */
73
        $contentType = $this->contentTypeService
74
            ->loadContentType(
75
                $content->contentInfo->contentTypeId
76
            );
77
78
        $fieldDefinitions = $contentType->getFieldDefinitions();
79
        $attributeValues = [];
80
81
        foreach ($attributes as $attr) {
82
83
            $fieldDefinition = $this->getFieldDefinition($fieldDefinitions, $attr);
84
85
            if (!$fieldDefinition instanceof FieldDefinition) {
0 ignored issues
show
Bug introduced by
The class eZ\Publish\API\Repositor...entType\FieldDefinition does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
86
                continue;
87
            }
88
89
            $attributeValues[] = $this->mapAttribute($attr, $content, $fieldDefinition);
90
        }
91
92
        $user = $this->getUser($collection->getCreatorId());
93
94
        return new Collection(
95
            $collection->getId(),
96
            $content,
97
            $user,
98
            $this->getDateTime($collection->getCreated()),
99
            $this->getDateTime($collection->getModified()),
100
            $attributeValues
101
        );
102
    }
103
104
    public function mapAttribute(EzInfoCollectionAttribute $attribute, APIContent $content, FieldDefinition $fieldDefinition): Attribute
105
    {
106
        $classField = new Field();
107
        foreach ($content->getFields() as $field) {
108
            if ($field->id === $attribute->getContentObjectAttributeId()) {
109
                $classField = $field;
110
111
                break;
112
            }
113
        }
114
115
        $value = new AttributeValue($attribute->getDataInt(), $attribute->getDataFloat(), $attribute->getDataText());
116
        return new Attribute(
117
            $attribute->getId(),
118
            $classField,
119
            $fieldDefinition,
120
            $value
121
        );
122
    }
123
124
    private function getUser($userId): ?User
125
    {
126
        try {
127
            return $this->repository
128
                ->getUserService()
129
                ->loadUser($userId);
130
        } catch (NotFoundException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class eZ\Publish\API\Repositor...tions\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
131
        }
132
    }
133
134
    private function getDateTime(int $timestamp): DateTimeInterface
135
    {
136
        return DateTimeImmutable::createFromFormat('U', (string) $timestamp);
137
    }
138
139
    private function getFieldDefinition(FieldDefinitionCollection $fieldDefinitionCollection, EzInfoCollectionAttribute $attribute): ?FieldDefinition
140
    {
141
        /** @var FieldDefinitionCollection $collection */
142
        $collection = $fieldDefinitionCollection->filter(function(FieldDefinition $definition) use ($attribute) {
143
            return $definition->id === $attribute->getContentClassAttributeId();
144
        });
145
146
        if ($collection->isEmpty()) {
147
            return null;
148
        }
149
150
        return $collection->first();
151
    }
152
}
153