MemberFactory::getTransaction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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 (
82
            $transaction->getField('customer_email')
83
            && $transaction->getField('is_anonymous') == 0
84
        ) {
85
86
            /** @var $encryption - disable password encryption to prevent double encryption */
87
            $encryption = Config::inst()->get(Security::class, 'password_encryption_algorithm');
88
            Config::modify()->set(Security::class, 'password_encryption_algorithm', 'none');
89
90
            if (!$customer = Member::get()->filter('Email', $transaction->getField('customer_email'))->first()) {
91
                $customer = Member::create();
92
            }
93
94
            foreach ($this->config()->get('member_mapping') as $foxy => $ssFoxy) {
95
                if ($transaction->hasField($foxy)) {
96
                    $customer->{$ssFoxy} = $transaction->getField($foxy);
97
                }
98
            }
99
100
            $doubleWrite = $customer->isChanged('Password');
101
            /** flag to prevent push to Foxy on write */
102
            $customer->FromDataFeed = true;
103
            $customer->write();
104
105
            if ($doubleWrite) {
106
                /** flag to prevent push to Foxy on write */
107
                $customer->FromDataFeed = true;
108
                $salt = $transaction->getField('customer_password_salt');
109
110
                $customer->Salt = $salt;
111
                /** manuall set encryption type to sha1 */
112
                $customer->PasswordEncryption = 'sha1_v2.4';
113
                $customer->write();
114
            }
115
116
            /** re-enable password encryption */
117
            Config::modify()->set(Security::class, 'password_encryption_algorithm', $encryption);
118
        }
119
    }
120
}
121