Passed
Pull Request — master (#30)
by Matthew
01:53
created

InstanceCreator::setFetcher()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 2
nc 1
nop 0
1
<?php
2
3
namespace Dynamic\Salsify\Traits;
4
5
use Dynamic\Salsify\Model\Fetcher;
6
use Dynamic\Salsify\Model\Mapper;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\Security\Member;
9
use SilverStripe\Security\Security;
10
11
/**
12
 * Trait InstanceCreator
13
 */
14
trait InstanceCreator
15
{
16
    /**
17
     * @var Fetcher
18
     */
19
    private $fetcher;
20
21
    /**
22
     * @var Mapper
23
     */
24
    private $mapper;
25
26
    /**
27
     * @var Member
28
     */
29
    private $previousUser;
30
31
    /**
32
     * @return string
33
     */
34
    abstract protected function getImporterKey();
35
36
    /**
37
     * @return string
38
     */
39
    protected function getMapperInstanceString()
40
    {
41
        return Mapper::class . '.' . $this->getImporterKey();
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    protected function getFetcherInstanceString()
48
    {
49
        return Fetcher::class . '.' . $this->getImporterKey();
50
    }
51
52
    /**
53
     * @return \SilverStripe\ORM\DataObject|Member
54
     * @throws \SilverStripe\ORM\ValidationException
55
     */
56
    protected function findOrCreateSalsifyUser() {
57
        if ($member = Member::get()->filter('Email', 'salsify')->first()) {
58
            return $member;
59
        }
60
61
        $member = Member::create();
62
        $member->FirstName = 'Salsify';
63
        $member->Surname = 'Integration';
64
        $member->Email = 'salsify';
65
        $member->write();
66
67
        return $member;
68
    }
69
70
    /**
71
     *
72
     */
73
    protected function changeToSalsifyUser()
74
    {
75
        $this->previousUser = Security::getCurrentUser();
76
        return Security::setCurrentUser($this->findOrCreateSalsifyUser());
0 ignored issues
show
Bug introduced by
Are you sure the usage of SilverStripe\Security\Se...dOrCreateSalsifyUser()) targeting SilverStripe\Security\Security::setCurrentUser() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
77
    }
78
79
    /**
80
     *
81
     */
82
    protected function changeToPreviousUser()
83
    {
84
        Security::setCurrentUser($this->previousUser);
85
        $this->previousUser = null;
86
    }
87
88
    /**
89
     * @param $className
90
     * @return bool
91
     */
92
    protected function hasService($className)
93
    {
94
        return Injector::inst()->has($className . '.' . $this->getImporterKey());
95
    }
96
97
    public function createServices()
98
    {
99
        if (!Injector::inst()->has($this->getMapperInstanceString())) {
100
            Injector::inst()->load([
101
                $this->getMapperInstanceString() => [
102
                    'class' => Mapper::class,
103
                ],
104
            ]);
105
        }
106
        if (!Injector::inst()->has($this->getFetcherInstanceString())) {
107
            Injector::inst()->load([
108
                $this->getFetcherInstanceString() => [
109
                    'class' => Fetcher::class,
110
                ],
111
            ]);
112
        }
113
    }
114
115
    /**
116
     * @return Fetcher
117
     * @throws \Exception
118
     */
119
    public function getFetcher()
120
    {
121
        if (!$this->fetcher) {
122
            $this->setFetcher();
123
        }
124
        return $this->fetcher;
125
    }
126
127
    /**
128
     * @throws \Exception
129
     */
130
    protected function setFetcher()
131
    {
132
        $this->fetcher = Injector::inst()->createWithArgs($this->getFetcherInstanceString(), [
133
            'importerKey' => $this->getImporterKey(),
134
            'noChannel' => property_exists($this, 'noChannel') ? $this->noChannel : false,
135
        ]);
136
    }
137
138
    /**
139
     * @return Mapper
140
     * @throws \Exception
141
     */
142
    public function getMapper()
143
    {
144
        if (!$this->mapper) {
145
            $this->setMapper();
146
        }
147
        return $this->mapper;
148
    }
149
150
    /**
151
     * @throws \Exception
152
     */
153
    protected function setMapper()
154
    {
155
        $this->mapper = Injector::inst()->createWithArgs($this->getMapperInstanceString(), [
156
            'importerKey' => $this->getImporterKey(),
157
            'file' => property_exists($this, 'file') ? $this->file : null,
158
        ]);
159
160
        $configKeys = [
161
            'apiKey',
162
            'timeout',
163
            'organizationID',
164
        ];
165
        for ($i = 0; $i < count($configKeys); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
166
            $currentKey = $configKeys[$i];
167
            $fetcherConfig = $this->getFetcher()->config()->get($currentKey);
168
            $this->mapper->config()->set($currentKey, $fetcherConfig);
169
        }
170
    }
171
}
172