Completed
Push — development ( ef0625...f79737 )
by Torben
04:11
created

PrefillFieldViewHelper::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace DERHANSEN\SfEventMgt\ViewHelpers\Registration\Field;
3
4
/*
5
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use DERHANSEN\SfEventMgt\Domain\Model\Registration\Field;
12
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
13
14
/**
15
 * PrefillField ViewHelper for registration fields
16
 *
17
 * @author Torben Hansen <[email protected]>
18
 */
19
class PrefillFieldViewHelper extends AbstractViewHelper
20
{
21
    /**
22
     * Initialize arguments
23
     */
24
    public function initializeArguments()
25
    {
26
        parent::initializeArguments();
27
        $this->registerArgument('registrationField', 'object', 'The registrationField object', true);
28
    }
29
30
    /**
31
     * Returns a string to be used as prefill value for the given registration field (type=input). If the form
32
     * has already been submitted, the submitted value for the field is returned.
33
     *
34
     * @return string
35
     */
36
    public function render()
37
    {
38
        /** @var Field $registrationField */
39
        $registrationField = $this->arguments['registrationField'];
40
        // If mapping errors occured for form, return value that has been submitted
41
        $originalRequest = $this->getRequest()->getOriginalRequest();
42
        if ($originalRequest) {
43
            return $this->getFieldValueFromArguments($originalRequest->getArguments(), $registrationField->getUid());
44
        }
45
46
        return $registrationField->getDefaultValue();
47
    }
48
49
    /**
50
     * Returns the submitted value for the given field uid
51
     *
52
     * @param array $submittedValues
53
     * @param int $fieldUid
54
     * @return string
55
     */
56
    protected function getFieldValueFromArguments($submittedValues, $fieldUid)
57
    {
58
        $result = '';
59
        foreach ($submittedValues['registration']['fieldValues'] as $fieldValue) {
60
            if ((int)$fieldValue['field'] === $fieldUid) {
61
                $result = $fieldValue['value'];
62
            }
63
        }
64
        return $result;
65
    }
66
67
    /**
68
     * Shortcut for retrieving the request from the controller context
69
     *
70
     * @return \TYPO3\CMS\Extbase\Mvc\Request
71
     */
72
    protected function getRequest()
73
    {
74
        return $this->renderingContext->getControllerContext()->getRequest();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TYPO3Fluid\Fluid\Core\Re...nderingContextInterface as the method getControllerContext() does only exist in the following implementations of said interface: Nimut\TestingFramework\R...RenderingContextFixture, TYPO3\CMS\Fluid\Core\Rendering\RenderingContext.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
75
    }
76
}
77