ImportBuilder::build()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9.1535

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 11
cts 17
cp 0.6471
rs 8.4746
c 0
b 0
f 0
cc 7
nc 7
nop 4
crap 9.1535
1
<?php
2
3
namespace Mathielen\ImportEngine\Import;
4
5
use Mathielen\ImportEngine\Event\ImportRequestEvent;
6
use Mathielen\ImportEngine\Exception\InvalidConfigurationException;
7
use Mathielen\ImportEngine\Importer\Importer;
8
use Mathielen\ImportEngine\Importer\ImporterRepository;
9
use Mathielen\ImportEngine\ValueObject\ImportConfiguration;
10
use Mathielen\ImportEngine\Storage\StorageInterface;
11
use Mathielen\ImportEngine\Storage\StorageLocator;
12
use Mathielen\ImportEngine\Event\ImportConfigureEvent;
13
use Mathielen\ImportEngine\ValueObject\ImportRequest;
14
use Mathielen\ImportEngine\ValueObject\ImportRun;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
17
class ImportBuilder
18
{
19
    /**
20
     * @var ImporterRepository
21
     */
22
    private $importerRepository;
23
24
    /**
25
     * @var StorageLocator
26
     */
27
    private $storageLocator;
28
29
    /**
30
     * @var EventDispatcherInterface
31
     */
32
    private $eventDispatcher;
33
34 1
    public function __construct(
35
        ImporterRepository $importerRepository,
36
        StorageLocator $storageLocator,
37
        EventDispatcherInterface $eventDispatcher = null)
38
    {
39 1
        $this->importerRepository = $importerRepository;
40 1
        $this->storageLocator = $storageLocator;
41 1
        $this->eventDispatcher = $eventDispatcher;
42 1
    }
43
44
    /**
45
     * @return null|string
46
     */
47
    public function findImporterForStorage(StorageInterface $storage)
48
    {
49
        return $this->importerRepository->find($storage);
50
    }
51
52
    /**
53
     * @return Import
54
     */
55
    public function rebuild(ImportRun $importRun)
56
    {
57
        $importerId = $importRun->getConfiguration()->getImporterId();
58
59
        $importer = $this->importerRepository->get($importerId);
60
        if ($importer->getSourceStorage()) {
61
            $sourceStorage = $importer->getSourceStorage();
62
        } else {
63
            $sourceStorage = $this->storageLocator->getStorage($importRun->getConfiguration()->getSourceStorageSelection());
64
        }
65
66
        return $this->factorImport($importer, $sourceStorage, $importRun);
67
    }
68
69
    /**
70
     * @return Import
71
     *
72
     * @throws InvalidConfigurationException
73
     */
74 1
    public function buildFromRequest(ImportRequest $importRequest)
75
    {
76 1
        $importerId = $importRequest->getImporterId();
77 1
        $storageSelection = null;
78 1
        $sourceStorage = null;
79
80 1
        if ($importRequest->hasSource()) {
81 1
            $storageSelection = $this->storageLocator->selectStorage(
82 1
                $importRequest->getSourceProviderId(),
83 1
                $importRequest->getSourceId()
84
            );
85
86 1
            $sourceStorage = $this->storageLocator->getStorage($storageSelection);
87
88 1
            if (!$importRequest->hasImporterId()) {
89
                $importerId = $this->findImporterForStorage($sourceStorage);
90
                if (!$importerId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $importerId of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
91
                    throw new InvalidConfigurationException('No importerId was given and there is no importer that matches the storage.');
92
                }
93
94
                $importRequest->setImporterId($importerId);
95
                $this->eventDispatcher->dispatch(
96
                    ImportRequestEvent::DISCOVERED,
97
                    new ImportRequestEvent($importRequest));
0 ignored issues
show
Documentation introduced by
new \Mathielen\ImportEng...stEvent($importRequest) is of type object<Mathielen\ImportE...ent\ImportRequestEvent>, but the function expects a null|string.

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...
98
            }
99
        }
100
101 1
        $importConfiguration = new ImportConfiguration($storageSelection, $importerId);
102
103 1
        return $this->build($importConfiguration, $sourceStorage, $importRequest->getCreatedBy(), $importRequest->getContext());
104
    }
105
106
    /**
107
     * @return Import
108
     *
109
     * @throws InvalidConfigurationException
110
     */
111 1
    public function build(ImportConfiguration $importConfiguration, StorageInterface $sourceStorage = null, $createdBy = null, $requestContext = null)
112
    {
113 1
        $importer = $this->importerRepository->get($importConfiguration->getImporterId());
114 1
        if ($importer->getSourceStorage()) {
115
            $sourceStorage = $importer->getSourceStorage();
116 1
        } elseif (!$sourceStorage) {
117
            throw new InvalidConfigurationException("Either the importRequest or the importer '".$importConfiguration->getImporterId()."' must have a source storage set.");
118
        }
119
120 1
        $importRun = $importConfiguration->toRun($createdBy);
121
122
        //apply static context from importer & request
123 1
        $context = $requestContext;
124 1
        if (!is_null($importer->getContext()) && !empty($importer->getContext())) {
125
            $importerContext = $importer->getContext();
126
            if ($importerContext && $requestContext) {
127
                $context = array_merge($importerContext, $requestContext);
128
            } else {
129
                $context = $importerContext;
130
            }
131
        }
132 1
        $importRun->setContext($context);
133
134 1
        $import = $this->factorImport($importer, $sourceStorage, $importRun);
135
136
        //after everthing was build, apply softdata from sourcestorage to importrun
137
        //dont do this any earlier, as there might be AFTER_BUILD hooks, that may change
138
        //the sourcestorage configuration
139 1
        $importRun->setInfo((array) $sourceStorage->info());
140
141 1
        return $import;
142
    }
143
144
    /**
145
     * @return Import
146
     */
147 1
    private function factorImport(Importer $importer, StorageInterface $sourceStorage, ImportRun $importRun)
148
    {
149 1
        $import = Import::build($importer, $sourceStorage, $importRun);
150
151
        //notify system
152 1
        if ($this->eventDispatcher) {
153
            $this->eventDispatcher->dispatch(
154
                ImportConfigureEvent::AFTER_BUILD,
155
                new ImportConfigureEvent($import));
0 ignored issues
show
Documentation introduced by
new \Mathielen\ImportEng...ConfigureEvent($import) is of type object<Mathielen\ImportE...t\ImportConfigureEvent>, but the function expects a null|string.

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...
156
157
            $this->eventDispatcher->dispatch(
158
                ImportConfigureEvent::AFTER_BUILD.'.'.$importRun->getConfiguration()->getImporterId(),
0 ignored issues
show
Documentation introduced by
\Mathielen\ImportEngine\...tion()->getImporterId() is of type string, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

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...
159
                new ImportConfigureEvent($import));
0 ignored issues
show
Documentation introduced by
new \Mathielen\ImportEng...ConfigureEvent($import) is of type object<Mathielen\ImportE...t\ImportConfigureEvent>, but the function expects a null|string.

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...
160
        }
161
162 1
        return $import;
163
    }
164
}
165