GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AjaxController::getFormManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Ajax controller. Used for validating forms through ajax
4
 *
5
 * @category  StrokerForm
6
 * @package   StrokerForm\Controller
7
 * @copyright 2012 Bram Gerritsen
8
 * @version   SVN: $Id$
9
 */
10
11
namespace StrokerForm\Controller;
12
13
use StrokerForm\FormManager;
14
use Zend\Json\Json;
15
use Zend\Mvc\Controller\AbstractActionController;
16
17
class AjaxController extends AbstractActionController
18
{
19
    /**
20
     * @var FormManager
21
     */
22
    protected $formManager;
23
24
    /**
25
     * Default constructor
26
     *
27
     * @param FormManager $formManager
28
     */
29
    public function __construct(FormManager $formManager)
30
    {
31
        $this->setFormManager($formManager);
32
    }
33
34
    /**
35
     * Validate a field and return validation messages on failure
36
     *
37
     * @throws \InvalidArgumentException
38
     * @return \Zend\Stdlib\ResponseInterface
39
     */
40
    public function validateAction()
41
    {
42
        /** @var $request \Zend\Http\PhpEnvironment\Request */
43
        $request = $this->getRequest();
44
        $response = $this->getResponse();
45
46
        $data = $request->getPost()->toArray();
47
48
        if (count($data) > 1) {
49
            throw new \InvalidArgumentException('Validating multiple fields is not allowed');
50
        }
51
        if (empty($data)) {
52
            throw new \InvalidArgumentException('No input data received');
53
        }
54
55
        $formAlias = $this->getEvent()->getRouteMatch()->getParam('form');
56
57
        /** @var $form \Zend\Form\FormInterface */
58
        $form = $this->getFormManager()->get($formAlias);
59
60
        $filter = $form->getInputFilter();
61
        $filter->setData($data);
62
        $filter->setValidationGroup($this->convertDataArrayToValidationGroup($data));
63
        $valid = $filter->isValid();
64
65
        if (!$valid) {
66
            $messages = $filter->getMessages();
67
68
            $result = false;
69
            array_walk_recursive(
70
                $messages, function ($item) use (&$result) {
71
                    if (is_string($item)) {
72
                        $result = $item;
73
                    }
74
                }
75
            );
76
        } else {
77
            $result = true;
78
        }
79
80
        $response->setContent(Json::encode($result));
81
82
        return $response;
83
    }
84
85
    /**
86
     * Convert post data to a format for the validation group
87
     *
88
     * i.e. from:
89
     *   array('fieldset' => array('field1' => 'field value'))
90
     * to:
91
     *   array('fieldset' => array('field1'))
92
     *
93
     * @param $data
94
     *
95
     * @return array
96
     */
97
    protected function convertDataArrayToValidationGroup($data)
98
    {
99
        $ret = [];
100
        foreach ($data as $key => $value) {
101
            if (is_array($value)) {
102
                $ret[$key] = $this->convertDataArrayToValidationGroup($value);
103
            } else {
104
                $ret[] = $key;
105
            }
106
        }
107
        return $ret;
108
    }
109
110
    /**
111
     * @return FormManager
112
     */
113
    public function getFormManager()
114
    {
115
        return $this->formManager;
116
    }
117
118
    /**
119
     * @param FormManager $formManager
120
     */
121
    public function setFormManager(FormManager $formManager)
122
    {
123
        $this->formManager = $formManager;
124
    }
125
}
126