Completed
Push — master ( 3c661d...2a57f9 )
by Will
26s queued 12s
created

src/Checkout/ShopMemberFactory.php (1 issue)

1
<?php
2
3
namespace SilverShop\Checkout;
4
5
use SilverShop\Extension\MemberExtension;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\ORM\ValidationException;
8
use SilverStripe\ORM\ValidationResult;
9
use SilverStripe\Security\Member;
10
11
class ShopMemberFactory
12
{
13
    /**
14
     * Create member account from data array.
15
     * Data must contain unique identifier.
16
     *
17
     * @throws ValidationException
18
     *
19
     * @param $data - map of member data
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
20
     *
21
     * @return Member - new member (not saved to db)
22
     */
23
    public function create($data)
24
    {
25
        $result = ValidationResult::create();
26
        if (!Checkout::member_creation_enabled()) {
27
            $result->addError(
28
                _t('SilverShop\Checkout\Checkout.MembershipIsNotAllowed', 'Creating new memberships is not allowed')
29
            );
30
            throw new ValidationException($result);
31
        }
32
        $idfield = Config::inst()->get(Member::class, 'unique_identifier_field');
33
        if (!isset($data[$idfield]) || empty($data[$idfield])) {
34
            $result->addError(
35
                _t(
36
                    'SilverShop\Checkout\Checkout.IdFieldNotFound',
37
                    'Required field not found: {IdentifierField}',
38
                    'Identifier is the field that holds the unique user-identifier, commonly this is \'Email\'',
39
                    ['IdentifierField' => $idfield]
40
                )
41
            );
42
            throw new ValidationException($result);
43
        }
44
        if (!isset($data['Password']) || empty($data['Password'])) {
45
            $result->addError(_t('SilverShop\Checkout\Checkout.PasswordRequired', 'A password is required'));
46
            throw new ValidationException($result);
47
        }
48
        $idval = $data[$idfield];
49
        if ($member = MemberExtension::get_by_identifier($idval)) {
50
            // get localized field labels
51
            $fieldLabels = $member->fieldLabels(false);
52
            // if a localized value exists, use this for our error-message
53
            $fieldLabel = isset($fieldLabels[$idfield]) ? $fieldLabels[$idfield] : $idfield;
54
55
            $result->addError(
56
                _t(
57
                    'SilverShop\Checkout\Checkout.MemberExists',
58
                    'A member already exists with the {Field} {Identifier}',
59
                    '',
60
                    ['Field' => $fieldLabel, 'Identifier' => $idval]
61
                )
62
            );
63
            throw new ValidationException($result);
64
        }
65
66
        /** @var Member $member */
67
        $member = Member::create()->update($data);
68
        // 3.2 changed validate to protected which made this fall through the DataExtension and error out
69
        $validation = $member->doValidate();
70
        if (!$validation->isValid()) {
71
            //TODO need to handle i18n here?
72
            foreach ($validation->getMessages() as $message) {
73
                $result->addError($message['message']);
74
            }
75
        }
76
        if (!$result->isValid()) {
77
            throw new ValidationException($result);
78
        }
79
80
        return $member;
81
    }
82
}
83