AdminController::uploadImage()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.3786
c 0
b 0
f 0
cc 7
nc 7
nop 1
1
<?php
2
3
namespace Publication\Controller;
4
5
use Application\Mvc\Controller;
6
use Publication\Model\Publication;
7
use Publication\Form\PublicationForm;
8
use Publication\Model\Type;
9
10
class AdminController extends Controller
11
{
12
13
    public function initialize()
14
    {
15
        $this->setAdminEnvironment();
16
        $this->helper->activeMenu()->setActive('admin-publication');
17
    }
18
19
    public function indexAction()
20
    {
21
        $page = $this->request->getQuery('page', 'int', 1);
22
        $type = $this->dispatcher->getParam('type');
0 ignored issues
show
Bug introduced by
The method getParam does only exist in Phalcon\Mvc\Dispatcher, but not in Phalcon\Mvc\DispatcherInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
23
        $type_id = null;
24
25
        $types = Type::find();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $types is correct as \Publication\Model\Type::find() (which targets Phalcon\Mvc\Model::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
26
27
        $cond_array = [];
28
        if ($type) {
29
            $typeEntity = Type::getCachedBySlug($type);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $typeEntity is correct as \Publication\Model\Type::getCachedBySlug($type) (which targets Publication\Model\Type::getCachedBySlug()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
30
            $type_id = $typeEntity->getId();
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $typeEntity (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
31
            $cond_array[] = "type_id = $type_id";
32
        }
33
34
        $conditions = implode(' AND ', $cond_array);
35
36
        $publications = Publication::find([
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $publications is correct as \Publication\Model\Publi... 'date DESC, id DESC')) (which targets Phalcon\Mvc\Model::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
37
            "conditions" => $conditions,
38
            "order"      => "date DESC, id DESC"
39
        ]);
40
41
        $paginator = new \Phalcon\Paginator\Adapter\Model([
42
            "data"  => $publications,
43
            "limit" => 20,
44
            "page"  => $page
45
        ]);
46
        $this->view->paginate = $paginator->getPaginate();
0 ignored issues
show
Bug introduced by
Accessing paginate on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Are you sure the assignment to $this->view->paginate is correct as $paginator->getPaginate() (which targets Phalcon\Paginator\Adapter\Model::getPaginate()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
47
48
        $this->view->types = $types;
0 ignored issues
show
Bug introduced by
Accessing types on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
49
        $this->view->type = $type;
0 ignored issues
show
Bug introduced by
Accessing type on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
50
        $this->view->type_id = $type_id;
0 ignored issues
show
Bug introduced by
Accessing type_id on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
51
52
        $this->helper->title($this->helper->at('Manage Publications'), true);
53
    }
54
55
    public function addAction()
56
    {
57
        $this->view->pick(['admin/edit']);
58
        $form = new PublicationForm();
59
        $model = new Publication();
60
61
        $type = $this->dispatcher->getParam('type');
0 ignored issues
show
Bug introduced by
The method getParam does only exist in Phalcon\Mvc\Dispatcher, but not in Phalcon\Mvc\DispatcherInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
62
        if ($type) {
63
            $typeEntity = Type::getCachedBySlug($type);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $typeEntity is correct as \Publication\Model\Type::getCachedBySlug($type) (which targets Publication\Model\Type::getCachedBySlug()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
64
            $form->get('type_id')->setDefault($typeEntity->getId());
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $typeEntity (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method setDefault cannot be called on $form->get('type_id') (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
65
        }
66
67
        if ($this->request->isPost()) {
68
            $post = $this->request->getPost();
69
            $form->bind($post, $model);
70
71
            if ($form->isValid()) {
72
                if ($model->create()) {
73
                    $form->bind($post, $model);
74
                    $model->updateFields($post);
75
                    if ($model->update()) {
76
                        $this->flash->success($this->helper->at('Publication created'));
77
                        return $this->redirect($this->url->get() . 'publication/admin/edit/' . $model->getId() . '?lang=' . LANG);
78
                    } else {
79
                        $this->flashErrors($model);
80
                    }
81
                } else {
82
                    $this->flashErrors($model);
83
                }
84
            } else {
85
                $this->flashErrors($form);
86
            }
87
        }
88
89
        $this->view->type = $type;
0 ignored issues
show
Bug introduced by
Accessing type on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
90
        $this->view->model = $model;
0 ignored issues
show
Bug introduced by
Accessing model on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
91
        $this->view->form = $form;
0 ignored issues
show
Bug introduced by
Accessing form on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
92
93
        $this->helper->title($this->helper->at('Create a publication'), true);
94
95
    }
96
97
    public function editAction($id)
98
    {
99
        $id = (int) $id;
100
        $form = new PublicationForm();
101
        $model = Publication::findFirst($id);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as \Publication\Model\Publication::findFirst($id) (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
102
103
        if ($model->getTypeId()) {
0 ignored issues
show
Bug introduced by
The method getTypeId cannot be called on $model (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
104
            $this->view->type = $model->getType()->getSlug();
0 ignored issues
show
Bug introduced by
Accessing type on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
The method getType cannot be called on $model (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
105
        }
106
107
        if ($this->request->isPost()) {
108
            $post = $this->request->getPost();
109
            $form->bind($post, $model);
110
            if ($form->isValid()) {
111
                $model->updateFields($post);
0 ignored issues
show
Bug introduced by
The method updateFields cannot be called on $model (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
112
                if ($model->save()) {
0 ignored issues
show
Bug introduced by
The method save cannot be called on $model (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
113
                    $this->uploadImage($model);
114
                    $this->flash->success($this->helper->at('Publication edited'));
115
116
                    return $this->redirect($this->url->get() . 'publication/admin/edit/' . $model->getId() . '?lang=' . LANG);
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $model (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
117
                } else {
118
                    $this->flashErrors($model);
119
                }
120
            } else {
121
                $this->flashErrors($form);
122
            }
123
        } else {
124
            $form->setEntity($model);
125
        }
126
127
        $this->view->model = $model;
0 ignored issues
show
Bug introduced by
Accessing model on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
128
        $this->view->form = $form;
0 ignored issues
show
Bug introduced by
Accessing form on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
129
        $this->helper->title($this->helper->at('Edit publication'), true);
130
    }
131
132
    public function deleteAction($id)
133
    {
134
        $model = Publication::findFirst($id);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as \Publication\Model\Publication::findFirst($id) (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
135
136
        if ($this->request->isPost()) {
137
            $model->delete();
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $model (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
138
            if ($model->getTypeId()) {
0 ignored issues
show
Bug introduced by
The method getTypeId cannot be called on $model (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
139
                $this->redirect($this->url->get() . 'publication/admin/' . $model->getType()->getSlug());
0 ignored issues
show
Bug introduced by
The method getType cannot be called on $model (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
140
            } else {
141
                $this->redirect($this->url->get() . 'publication/admin');
142
            }
143
        }
144
145
        $this->view->model = $model;
0 ignored issues
show
Bug introduced by
Accessing model on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
146
        $this->helper->title($this->helper->at('Unpublishing'), true);
147
    }
148
149
    private function uploadImage($model)
150
    {
151
        if ($this->request->isPost()) {
152
            if ($this->request->hasFiles() == true) {
153
                foreach ($this->request->getUploadedFiles() as $file) {
154
                    if (!$file->getTempName()) {
155
                        return;
156
                    }
157
                    if (!in_array($file->getType(), [
158
                        'image/bmp',
159
                        'image/jpeg',
160
                        'image/png',
161
                    ])
162
                    ) {
163
                        return $this->flash->error($this->helper->at('Only allow image formats jpg, jpeg, png, bmp'));
164
                    }
165
166
                    $imageFilter = new \Image\Storage([
167
                        'id'   => $model->getId(),
168
                        'type' => 'publication',
169
                    ]);
170
                    $imageFilter->removeCached();
171
172
                    $resize_x = 1000;
173
                    $image = new \Phalcon\Image\Adapter\GD($file->getTempName());
174
                    if ($image->getWidth() > $resize_x) {
175
                        $image->resize($resize_x);
176
                    }
177
                    $image->save($imageFilter->originalAbsPath());
178
179
                    $model->setPreviewSrc($imageFilter->originalRelPath());
180
                    $model->update();
181
182
                    $this->flash->success($this->helper->at('Photo added'));
183
                }
184
            }
185
        }
186
    }
187
188
}
189