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.

DomainObjectMapper   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 16
dl 0
loc 129
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A mapContent() 0 15 2
A mapCollection() 0 34 3
A mapAttribute() 0 19 3
A getUser() 0 9 2
A getDateTime() 0 4 1
A getFieldDefinition() 0 13 2
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) {
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...
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