| Conditions | 11 |
| Paths | 9 |
| Total Lines | 58 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 83 |