Completed
Push — master ( 19db0b...f5d9b1 )
by Mathieu
03:09
created

ObjectFormWidget::setDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Charcoal\Admin\Widget;
4
5
use \InvalidArgumentException;
6
use \Exception;
7
8
use \Pimple\Container;
9
10
use \Charcoal\Admin\Widget\FormWidget;
11
use \Charcoal\Admin\Widget\FormPropertyWidget;
12
13
use \Charcoal\Admin\Ui\ObjectContainerInterface;
14
use \Charcoal\Admin\Ui\ObjectContainerTrait;
15
16
/**
17
 *
18
 */
19
class ObjectFormWidget extends FormWidget implements ObjectContainerInterface
20
{
21
    use ObjectContainerTrait;
22
23
    /**
24
     * @var string
25
     */
26
    protected $formIdent;
27
28
    /**
29
     * @param Container $container The DI container.
30
     * @return void
31
     */
32
    public function setDependencies(Container $container)
33
    {
34
        parent::setDependencies($container);
35
36
        // Fill ObjectContainerInterface dependencies
37
        $this->setModelFactory($container['model/factory']);
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function widgetType()
44
    {
45
        return 'charcoal/admin/widget/objectForm';
46
    }
47
48
    /**
49
     * @param array|ArrayInterface $data The widget data.
50
     * @return ObjectForm Chainable
51
     */
52
    public function setData($data)
0 ignored issues
show
Coding Style introduced by
setData uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
53
    {
54
        // @TODO Remove once RequirementContainer is implemented
55
        // Needed this to be able to output {{objId}}
56
        $data = array_merge($_GET, $data);
57
58
        parent::setData($data);
59
60
        $objData = $this->dataFromObject();
61
        $data = array_merge_recursive($objData, $data);
62
63
        parent::setData($data);
64
65
        return $this;
66
    }
67
68
     /**
69
      * @param string $url The next URL.
70
      * @throws InvalidArgumentException If argument is not a string.
71
      * @return ActionInterface Chainable
72
      */
73
    public function setNextUrl($url)
74
    {
75
        if (!is_string($url)) {
76
            throw new InvalidArgumentException(
77
                'URL needs to be a string'
78
            );
79
        }
80
81
        if (!$this->obj()) {
82
            $this->nextUrl = $url;
0 ignored issues
show
Bug introduced by
The property nextUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
83
            return $this;
84
        }
85
86
        $this->nextUrl = $this->obj()->render($url);
87
        return $this;
88
    }
89
90
    /**
91
     * Form action (target URL)
92
     *
93
     * @return string Relative URL
94
     */
95
    public function action()
96
    {
97
        $action = parent::action();
98
        if (!$action) {
99
            $obj = $this->obj();
100
            $objId = $obj->id();
101
            if ($objId) {
102
                return 'action/object/update';
103
            } else {
104
                return 'action/object/save';
105
            }
106
        } else {
107
            return $action;
108
        }
109
    }
110
111
    /**
112
     * @param string $formIdent The form ident.
113
     * @throws InvalidArgumentException If the argument is not a string.
114
     * @return ObjectForm Chainable
115
     */
116
    public function setFormIdent($formIdent)
117
    {
118
        if (!is_string($formIdent)) {
119
            throw new InvalidArgumentException(
120
                'Form ident must be a string'
121
            );
122
        }
123
        $this->formIdent = $formIdent;
124
        return $this;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function formIdent()
131
    {
132
        return $this->formIdent;
133
    }
134
135
    /**
136
     * Set the data from an object.
137
     *
138
     * @return array
139
     */
140 View Code Duplication
    public function dataFromObject()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        $obj = $this->obj();
143
        $metadata = $obj->metadata();
144
        $admin_metadata = isset($metadata['admin']) ? $metadata['admin'] : null;
145
        $formIdent = $this->formIdent();
146
        if (!$formIdent) {
147
            $formIdent = isset($admin_metadata['default_form']) ? $admin_metadata['default_form'] : '';
148
        }
149
150
        $objFormData = isset($admin_metadata['forms'][$formIdent]) ? $admin_metadata['forms'][$formIdent] : [];
151
        return $objFormData;
152
    }
153
154
    /**
155
     * FormProperty Generator
156
     *
157
     * @param array $group An optional group to use.
158
     * @throws Exception If a property data is invalid.
159
     * @return void This is a generator.
160
     */
161
    public function formProperties(array $group = null)
162
    {
163
        $obj = $this->obj();
164
        $props = $obj->metadata()->properties();
165
166
        // We need to sort form properties by form group property order if a group exists
167
        if (!empty($group)) {
168
            $props = array_merge(array_flip($group), $props);
169
        }
170
171
        foreach ($props as $propertyIdent => $property) {
172 View Code Duplication
            if (!is_array($property)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
                throw new Exception(
174
                    sprintf(
175
                        'Invalid property data for "%1$s", received %2$s',
176
                        $propertyIdent,
177
                        (is_object($property) ? get_class($property) : gettype($property))
178
                    )
179
                );
180
            }
181
            $p = new FormPropertyWidget([
182
                'logger' => $this->logger
183
            ]);
184
            $p->setPropertyIdent($propertyIdent);
185
            $p->setData($property);
186
            yield $propertyIdent => $p;
187
        }
188
    }
189
190
    /**
191
     * @return array
192
     */
193
    public function formData()
194
    {
195
        $obj = $this->obj();
196
        $formData = $obj->data();
197
        return $formData;
198
    }
199
}
200