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.

ExporterService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Netgen\InformationCollection\Core\Service;
6
7
use Netgen\InformationCollection\API\Service\Exporter;
8
use Netgen\InformationCollection\API\Service\InformationCollection;
9
use Netgen\InformationCollection\API\Value\Attribute;
10
use Netgen\InformationCollection\API\Value\Export\Export;
11
use Netgen\InformationCollection\API\Value\Export\ExportCriteria;
12
use Netgen\InformationCollection\API\Value\Filter\ContentId;
13
use Netgen\InformationCollection\Core\Persistence\ContentTypeUtils;
14
use Symfony\Contracts\Translation\TranslatorInterface;
15
16
class ExporterService implements Exporter
17
{
18
    /**
19
     * @var \Symfony\Component\Translation\TranslatorInterface
20
     */
21
    protected $translator;
22
23
    /**
24
     * @var \Netgen\InformationCollection\Core\Persistence\ContentTypeUtils
25
     */
26
    protected $contentTypeUtils;
27
28
    /**
29
     * @var \Netgen\InformationCollection\API\Service\InformationCollection
30
     */
31
    protected $informationCollection;
32
33
    public function __construct(
34
        InformationCollection $informationCollection,
35
        TranslatorInterface $translator,
36
        ContentTypeUtils $contentTypeUtils
37
    ) {
38
        $this->translator = $translator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $translator of type object<Symfony\Contracts...on\TranslatorInterface> is incompatible with the declared type object<Symfony\Component...on\TranslatorInterface> of property $translator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
        $this->contentTypeUtils = $contentTypeUtils;
40
        $this->informationCollection = $informationCollection;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function export(ExportCriteria $criteria): Export
47
    {
48
        $fields = $this->contentTypeUtils
49
            ->getInfoCollectorFields($criteria->getContent()->id);
0 ignored issues
show
Bug introduced by
The method getContent() does not exist on Netgen\InformationCollec...e\Export\ExportCriteria. Did you maybe mean getContentId()?

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...
50
51
        $fields['created'] = $this->translator->trans('netgen_information_collection_admin_export_created', [], 'netgen_information_collection_admin');
52
53
        $collections = $this->informationCollection->getCollections(
54
            new ContentId($criteria->getContent()->id, $criteria->getOffset(), $criteria->getLimit())
0 ignored issues
show
Bug introduced by
The method getContent() does not exist on Netgen\InformationCollec...e\Export\ExportCriteria. Did you maybe mean getContentId()?

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...
Bug introduced by
The method getOffset() does not seem to exist on object<Netgen\Informatio...\Export\ExportCriteria>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getLimit() does not seem to exist on object<Netgen\Informatio...\Export\ExportCriteria>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
        );
56
57
        $rows = [];
58
59
        foreach ($collections->getCollections() as $collection) {
60
            $row = [];
61
62
            foreach ($fields as $fieldId => $fieldName) {
63
                if ($fieldId === 'created') {
64
                    $row[] = $collection->getCreated()->format('d-m-Y');
65
66
                    continue;
67
                }
68
69
                $row[] = $this->getAttributeValue((int)$fieldId, $collection->getAttributes());
70
            }
71
72
            $rows[] = $row;
73
        }
74
75
        $header = array_values($fields);
76
77
        return new Export($header, $rows);
78
    }
79
80
    /**
81
     * Get attribute value string.
82
     *
83
     * @param int $fieldId
84
     * @param array $attributes
85
     *
86
     * @return string
87
     */
88
    protected function getAttributeValue(int $fieldId, array $attributes)
89
    {
90
        /** @var Attribute $attribute */
91
        foreach ($attributes as $attribute) {
92
            if ($fieldId === $attribute->getFieldDefinition()->id) {
93
                $value = $attribute->getValue();
94
                $value = str_replace('"', '""', (string)$value);
95
                $value = str_replace(';', ', ', (string)$value);
96
                $value = strip_tags($value);
97
98
                $res = preg_replace(['/\r|\n/'], [' '], $value);
99
100
                return $res ?? '';
101
            }
102
        }
103
104
        return '';
105
    }
106
}
107