Completed
Pull Request — master (#12)
by Simon
01:19
created

PartialUserFormController::setData()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8017
c 0
b 0
f 0
cc 6
nc 3
nop 1
1
<?php
2
3
namespace Firesphere\PartialUserforms\Controllers;
4
5
use Firesphere\PartialUserforms\Forms\PasswordForm;
6
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
7
use Page;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Control\HTTPResponse;
10
use SilverStripe\Control\HTTPResponse_Exception;
11
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
12
use SilverStripe\ORM\DataObject;
13
use SilverStripe\ORM\FieldType\DBField;
14
use SilverStripe\ORM\FieldType\DBHTMLText;
15
use SilverStripe\UserForms\Control\UserDefinedFormController;
16
use SilverStripe\UserForms\Model\UserDefinedForm;
17
18
/**
19
 * Class PartialUserFormController
20
 *
21
 * @package Firesphere\PartialUserforms\Controllers
22
 */
23
class PartialUserFormController extends UserDefinedFormController
24
{
25
26
    /**
27
     * @var PartialFormSubmission
28
     */
29
    protected $partialFormSubmission;
30
    /**
31
     * @var array
32
     */
33
    private static $url_handlers = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
34
        '$Key/$Token' => 'partial',
35
    ];
36
37
    /**
38
     * @var array
39
     */
40
    private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
41
        'partial',
42
    ];
43
44
    /**
45
     * A little abstraction to be more readable
46
     *
47
     * @param HTTPRequest $request
48
     * @return PartialFormSubmission|void
49
     * @throws HTTPResponse_Exception
50
     */
51
    public function setData($request)
52
    {
53
        // Ensure this URL doesn't get picked up by HTTP caches
54
        HTTPCacheControlMiddleware::singleton()->disableCache();
55
56
        $key = $request->param('Key');
57
        $token = $request->param('Token');
58
59
        /** @var PartialFormSubmission $partial */
60
        $partial = PartialFormSubmission::get()->find('Token', $token);
61
        if (!$token ||
62
            !$partial ||
63
            !$partial->UserDefinedFormID ||
64
            !hash_equals($partial->generateKey($token), $key)
65
        ) {
66
            return $this->httpError(404);
67
        }
68
69
        $session = $this->getRequest()->getSession();
70
        // Set the session if the last session has expired
71
        if (!$session->get(PartialSubmissionController::SESSION_KEY)) {
72
            $session->set(PartialSubmissionController::SESSION_KEY, $partial->ID);
73
        }
74
75
        $this->setPartialFormSubmission($partial);
76
        // Set data record and load the form
77
        /** @var UserDefinedForm dataRecord */
78
        $this->dataRecord = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID);
0 ignored issues
show
Documentation introduced by
The property UserDefinedFormClass does not exist on object<Firesphere\Partia...\PartialFormSubmission>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation Bug introduced by
It seems like \SilverStripe\ORM\DataOb...ial->UserDefinedFormID) can also be of type object<SilverStripe\ORM\DataObject>. However, the property $dataRecord is declared as type object<SilverStripe\CMS\Model\SiteTree>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
79
80
        return $partial;
81
    }
82
83
    /**
84
     * Partial form
85
     *
86
     * @param HTTPRequest $request
87
     * @return HTTPResponse|DBHTMLText|void
88
     * @throws \Exception
89
     */
90
    public function partial(HTTPRequest $request)
91
    {
92
        /** @var PartialFormSubmission $partial */
93
        $partial = $this->setData($request);
94
        if ($this->dataRecord->PasswordProtected &&
95
            $request->getSession()->get(PasswordForm::PASSWORD_SESSION_KEY) !== $partial->ID
96
        ) {
97
            return $this->redirect('verify');
98
        }
99
100
//        $this->setFailover($this->dataRecord);
101
102
        $form = $this->Form();
103
        $fields = $partial->PartialFields()->map('Name', 'Value')->toArray();
104
        $form->loadDataFrom($fields);
0 ignored issues
show
Documentation Bug introduced by
The method loadDataFrom does not exist on object<Firesphere\Partia...tialUserFormController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
105
106
        // Copied from {@link UserDefinedFormController}
107
        if ($this->Content && $form && !$this->config()->disable_form_content_shortcode) {
0 ignored issues
show
Documentation introduced by
The property Content does not exist on object<Firesphere\Partia...tialUserFormController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
108
            $hasLocation = stristr($this->Content, '$UserDefinedForm');
0 ignored issues
show
Documentation introduced by
The property Content does not exist on object<Firesphere\Partia...tialUserFormController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
109
            if ($hasLocation) {
110
                /** @see Requirements_Backend::escapeReplacement */
111
                $formEscapedForRegex = addcslashes($form->forTemplate(), '\\$');
0 ignored issues
show
Documentation Bug introduced by
The method forTemplate does not exist on object<Firesphere\Partia...tialUserFormController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
112
                $content = preg_replace(
113
                    '/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i',
114
                    $formEscapedForRegex,
115
                    $this->Content
0 ignored issues
show
Documentation introduced by
The property Content does not exist on object<Firesphere\Partia...tialUserFormController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
116
                );
117
118
                return $this->customise([
119
                    'Content'     => DBField::create_field('HTMLText', $content),
120
                    'Form'        => '',
121
                    'PartialLink' => $partial->getPartialLink()
122
                ])->renderWith([static::class, Page::class]);
123
            }
124
        }
125
126
        return $this->customise([
127
            'Content'     => DBField::create_field('HTMLText', $this->Content),
0 ignored issues
show
Documentation introduced by
The property Content does not exist on object<Firesphere\Partia...tialUserFormController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
128
            'Form'        => $form,
129
            'PartialLink' => $partial->getPartialLink()
130
        ])->renderWith([static::class, Page::class]);
131
    }
132
133
    /**
134
     * @return PartialFormSubmission
135
     */
136
    public function getPartialFormSubmission(): PartialFormSubmission
137
    {
138
        return $this->partialFormSubmission;
139
    }
140
141
    /**
142
     * @param PartialFormSubmission $partialFormSubmission
143
     */
144
    public function setPartialFormSubmission(PartialFormSubmission $partialFormSubmission): void
145
    {
146
        $this->partialFormSubmission = $partialFormSubmission;
147
    }
148
}
149