Completed
Pull Request — develop (#359)
by Mathias
23:51
created

FileUploadFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 0
loc 148
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C createService() 0 81 8
A configureForm() 0 3 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/**  */
11
namespace Core\Form;
12
13
use Zend\ServiceManager\FactoryInterface;
14
use Zend\ServiceManager\ServiceLocatorInterface;
15
use Core\Entity\Hydrator\EntityHydrator;
16
use Core\Entity\Hydrator\Strategy\FileUploadStrategy;
17
use Auth\Entity\AnonymousUser;
18
use Core\Entity\Hydrator\FileCollectionUploadHydrator;
19
use Zend\Stdlib\AbstractOptions;
20
21
/**
22
 * Factory for creating file upload formular elements.
23
 *
24
 * @author Mathias Gelhausen <[email protected]>
25
 */
26
class FileUploadFactory implements FactoryInterface
27
{
28
    /**
29
     * Service name of the form element used.
30
     *
31
     * @var string
32
     */
33
    protected $fileElement = 'Core/FileUpload';
34
35
    /**
36
     * Name of the form element.
37
     *
38
     * @var string
39
     */
40
    protected $fileName = 'file';
41
42
    /**
43
     * Class name of the file entity to use.
44
     *
45
     * @var string
46
     */
47
    protected $fileEntityClass = '\Core\Form\FileEntity';
48
49
    /**
50
     * Should the factored element allow multiple files to be selected?
51
     *
52
     * @var bool
53
     */
54
    protected $multiple = false;
55
56
    /**
57
     * The config key in the FileUploadFactory configuration entry.
58
     * @var string
59
     */
60
    protected $configKey;
61
62
    /**
63
     * The configuration stored in the FileUploadFactory configuration entry under {@link $configKey}.
64
     *
65
     * @var array|null
66
     */
67
    protected $config;
68
69
    /**
70
     * Optional Module Options. eg. "Jobs/Options" or "Applications/Options"
71
     *
72
     * @var string|null
73
     */
74
    protected $options;
75
76
    /**
77
     * The configuration from the FileUploadFactory configuration
78
     *
79
     * @param ServiceLocatorInterface $serviceLocator
80
     * @return Form
81
     */
82
    public function createService(ServiceLocatorInterface $serviceLocator)
83
    {
84
        /* @var $serviceLocator \Zend\Form\FormElementManager */
85
        $service = $serviceLocator->getServiceLocator();
86
        $options=null;
87
        if ($this->options) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->options of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
88
            $options = $service->get($this->options);
89
        }
90
91
        // Retrieve configuration.
92
        if ($this->configKey) {
93
            $services = $serviceLocator->getServiceLocator();
94
            $config   = $services->get('config');
95
            $config   = isset($config['form_elements_config']['file_upload_factories'][$this->configKey])
96
                      ? $config['form_elements_config']['file_upload_factories'][$this->configKey]
97
                      : array();
98
            $this->config = $config;
99
        }
100
101
102
        $form = new Form();
103
        $serviceLocator->injectFactory($form, $serviceLocator);
104
        $form->add(
105
            array(
106
            'type' => $this->fileElement,
107
            'name' => $this->fileName,
108
            'options' => array(
109
                'use_formrow_helper' => false,
110
            ),
111
            'attributes' => array(
112
                'class' => 'hide',
113
            ),
114
            )
115
        );
116
        /* @var $element \Core\Form\Element\FileUpload */
117
        $element = $form->get($this->fileName);
118
        $element->setIsMultiple($this->multiple);
119
        
120
        $user = $serviceLocator->getServiceLocator()->get('AuthenticationService')->getUser();
121
122
        if (isset($this->config['hydrator']) && $this->config['hydrator']) {
123
            /** @noinspection PhpUndefinedVariableInspection */
124
            $hydrator = $services->get('HydratorManager')->get($this->config['hydrator']);
0 ignored issues
show
Bug introduced by
The variable $services does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
125
        } else {
126
127
128
            /* @var $fileEntity \Core\Entity\FileInterface */
129
            $fileEntity = new $this->fileEntityClass();
130
            if ($user instanceof AnonymousUser) {
131
                $fileEntity->getPermissions()->grant($user, 'all');
132
            } else {
133
                $fileEntity->setUser($user);
134
            }
135
136
            $strategy = new FileUploadStrategy($fileEntity);
137
            if ($this->multiple) {
138
                $hydrator = new FileCollectionUploadHydrator($this->fileName, $strategy);
139
                $form->add(
140
                    array(
141
                    'type' => 'button',
142
                    'name' => 'remove',
143
                    'options' => array(
144
                        'label' => /*@translate*/ 'Remove all',
145
                    ),
146
                    'attributes' => array(
147
                        'class' => 'fu-remove-all btn btn-danger btn-xs pull-right'
148
                    ),
149
                    )
150
                );
151
            } else {
152
                $hydrator = new EntityHydrator();
153
                $hydrator->addStrategy($this->fileName, $strategy);
154
            }
155
        }
156
157
        $form->setHydrator($hydrator);
158
        $form->setOptions(array('use_files_array' => true));
159
        
160
        $this->configureForm($form, $options);
161
        return $form;
162
    }
163
164
    /**
165
     * Configures the factored form.
166
     *
167
     * @param \Core\Form\Form $form
168
     * @param AbstractOptions $options
169
     */
170
    protected function configureForm($form, AbstractOptions $options)
171
    {
172
    }
173
}
174