Passed
Pull Request — master (#14)
by Nic
01:59
created

MemberFactory::setTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Dynamic\Foxy\SingleSignOn\Factory;
4
5
use Dynamic\Foxy\Orders\Factory\FoxyFactory;
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Orders\Factory\FoxyFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Dynamic\Foxy\Parser\Foxy\Transaction;
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Parser\Foxy\Transaction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Config\Configurable;
9
use SilverStripe\Core\Extensible;
10
use SilverStripe\Core\Injector\Injectable;
11
use SilverStripe\Security\Member;
12
use SilverStripe\Security\Security;
13
use SilverStripe\View\ArrayData;
14
use Psr\Log\LoggerInterface;
15
use SilverStripe\Core\Injector\Injector;
16
17
class MemberFactory
18
{
19
    use Configurable;
20
    use Extensible;
21
    use Injectable;
22
23
    /**
24
     * @var Transaction
25
     */
26
    private $transaction;
27
28
    /**
29
     * @var
30
     */
31
    private $member;
32
33
    /**
34
     * OrderDetailFactory constructor.
35
     * @param Transaction|null $transaction
36
     */
37
    public function __construct(Transaction $transaction = null)
38
    {
39
        if ($transaction instanceof Transaction && $transaction !== null) {
40
            $this->setTransaction($transaction);
41
        }
42
    }
43
44
    /**
45
     * @param Transaction $transaction
46
     * @return $this
47
     */
48
    public function setTransaction(Transaction $transaction)
49
    {
50
        $this->transaction = $transaction;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @return Transaction
57
     */
58
    protected function getTransaction()
59
    {
60
        return $this->transaction;
61
    }
62
63
    /**
64
     * @return Member
65
     */
66
    public function getMember()
67
    {
68
        if (!$this->member instanceof Member) {
69
            $this->setMember();
70
        }
71
72
        return $this->member;
73
    }
74
75
    protected function setMember()
76
    {
77
        /** @var ArrayData $transaction */
78
        $transaction = $this->getTransaction()->getParsedTransactionData()->getField('transaction');
79
80
        // if not a guest transaction in Foxy
81
        if ($transaction->getField('customer_email')
82
            && $transaction->getField('is_anonymous') == 0) {
83
84
            /** @var $encryption - disable password encryption to prevent double encryption */
85
            $encryption = Config::inst()->get(Security::class, 'password_encryption_algorithm');
86
            Config::modify()->set(Security::class, 'password_encryption_algorithm', 'none');
87
88
            if (!$customer = Member::get()->filter('Email', $transaction->getField('customer_email'))->first()) {
89
                $customer = Member::create();
90
            }
91
92
            foreach ($this->config()->get('member_mapping') as $foxy => $ssFoxy) {
93
                if ($transaction->hasField($foxy)) {
94
                    $customer->{$ssFoxy} = $transaction->getField($foxy);
95
                }
96
            }
97
98
            $doubleWrite = $customer->isChanged('Password');
99
            /** flag to prevent push to Foxy on write */
100
            $customer->FromDataFeed = true;
101
            $customer->write();
102
103
            if ($doubleWrite) {
104
                /** flag to prevent push to Foxy on write */
105
                $customer->FromDataFeed = true;
106
                $salt = $transaction->getField('customer_password_salt');
107
108
                $customer->Salt = $salt;
109
                /** manuall set encryption type to sha1 */
110
                $customer->PasswordEncryption = 'sha1_v2.4';
111
                $customer->write();
112
            }
113
114
            /** re-enable password encryption */
115
            Config::modify()->set(Security::class, 'password_encryption_algorithm', $encryption);
116
        }
117
    }
118
}
119