Passed
Push — master ( e5d815...0b1635 )
by Matthew
01:47
created

InstanceCreator::changeToSalsifyUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
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
    {
58
        if ($member = Member::get()->filter('Email', 'salsify')->first()) {
59
            return $member;
60
        }
61
62
        $member = Member::create();
63
        $member->FirstName = 'Salsify';
64
        $member->Surname = 'Integration';
65
        $member->Email = 'salsify';
66
        $member->write();
67
68
        return $member;
69
    }
70
71
    /**
72
     *
73
     */
74
    protected function changeToSalsifyUser()
75
    {
76
        $this->previousUser = Security::getCurrentUser();
77
        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...
78
    }
79
80
    /**
81
     *
82
     */
83
    protected function changeToPreviousUser()
84
    {
85
        Security::setCurrentUser($this->previousUser);
86
        $this->previousUser = null;
87
    }
88
89
    /**
90
     * @param $className
91
     * @return bool
92
     */
93
    protected function hasService($className)
94
    {
95
        return Injector::inst()->has($className . '.' . $this->getImporterKey());
96
    }
97
98
    public function createServices()
99
    {
100
        if (!Injector::inst()->has($this->getMapperInstanceString())) {
101
            Injector::inst()->load([
102
                $this->getMapperInstanceString() => [
103
                    'class' => Mapper::class,
104
                ],
105
            ]);
106
        }
107
        if (!Injector::inst()->has($this->getFetcherInstanceString())) {
108
            Injector::inst()->load([
109
                $this->getFetcherInstanceString() => [
110
                    'class' => Fetcher::class,
111
                ],
112
            ]);
113
        }
114
    }
115
116
    /**
117
     * @return Fetcher
118
     * @throws \Exception
119
     */
120
    public function getFetcher()
121
    {
122
        if (!$this->fetcher) {
123
            $this->setFetcher();
124
        }
125
        return $this->fetcher;
126
    }
127
128
    /**
129
     * @throws \Exception
130
     */
131
    protected function setFetcher()
132
    {
133
        $this->fetcher = Injector::inst()->createWithArgs($this->getFetcherInstanceString(), [
134
            'importerKey' => $this->getImporterKey(),
135
            'noChannel' => property_exists($this, 'noChannel') ? $this->noChannel : false,
136
        ]);
137
    }
138
139
    /**
140
     * @return Mapper
141
     * @throws \Exception
142
     */
143
    public function getMapper()
144
    {
145
        if (!$this->mapper) {
146
            $this->setMapper();
147
        }
148
        return $this->mapper;
149
    }
150
151
    /**
152
     * @throws \Exception
153
     */
154
    protected function setMapper()
155
    {
156
        $this->mapper = Injector::inst()->createWithArgs($this->getMapperInstanceString(), [
157
            'importerKey' => $this->getImporterKey(),
158
            'file' => property_exists($this, 'file') ? $this->file : null,
159
        ]);
160
161
        $configKeys = [
162
            'apiKey',
163
            'timeout',
164
            'organizationID',
165
        ];
166
        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...
167
            $currentKey = $configKeys[$i];
168
            $fetcherConfig = $this->getFetcher()->config()->get($currentKey);
169
            $this->mapper->config()->set($currentKey, $fetcherConfig);
170
        }
171
    }
172
}
173