Completed
Push — master ( 3e0714...50ebf9 )
by Gabriel
09:17
created

AdminPdfLetterControllerTrait::upload()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 18
cp 0
rs 8.8177
c 0
b 0
f 0
cc 6
nc 10
nop 0
crap 42
1
<?php
2
3
namespace ByTIC\DocumentGenerator\PdfLetters\Controllers;
4
5
use ByTIC\DocumentGenerator\PdfLetters\PdfLettersTrait;
6
use ByTIC\DocumentGenerator\PdfLetters\PdfLetterTrait;
7
use ByTIC\MediaLibrary\Media\Media;
8
use Nip\Controllers\Traits\AbstractControllerTrait;
9
use Nip\Records\Record;
10
use Nip\Records\RecordManager;
11
use Symfony\Component\HttpFoundation\File\UploadedFile;
12
13
/**
14
 * Trait AdminPdfLetterControllerTrait
15
 * @package ByTIC\DocumentGenerator\PdfLetters
16
 *
17
 * @method PdfLetterTrait getModelFromRequest()
18
 * @method PdfLettersTrait getModelManager()
19
 */
20
trait AdminPdfLetterControllerTrait
21
{
22
    use AbstractControllerTrait;
23
24
    /**
25
     * @var string
26
     */
27
    protected $letterType;
28
29
    /**
30
     * @var RecordManager
31
     */
32
    protected $parentManager;
33
34
    /**
35
     * @var Record
36
     */
37
    protected $parent;
38
39
40
    public function upload()
41
    {
42
        if ($_FILES['letter']) {
43
            $parent = $this->getParent();
44
            if ($parent) {
45
                /** @var UploadedFile $uploadedFile */
46
                $uploadedFile = $this->getRequest()->files->get('letter');
47
48
                if (!$uploadedFile->isValid()) {
49
                    $this->flashRedirectLetter($parent, $uploadedFile->getErrorMessage(), 'error');
50
                }
51
52
                $letter = $this->getModelFromRequest();
53
                if (!$letter) {
54
                    $letter = $this->newPdfLetterRecordFromItemType($parent, $this->getLetterType());
55
                }
56
57
                $result = $letter->uploadFromRequest($uploadedFile);
0 ignored issues
show
Bug introduced by
The method uploadFromRequest does only exist in ByTIC\DocumentGenerator\PdfLetters\PdfLetterTrait, but not in Nip\Records\Record.

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...
58
59
                if ($result instanceof Media) {
60
                    $this->flashRedirectLetter($parent, $this->getModelManager()->getMessage('add'));
0 ignored issues
show
Bug introduced by
It seems like getMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
61
                } else {
62
                    $letter->delete();
63
                    $this->flashRedirectLetter($parent, $result, 'error');
64
                }
65
                die('end');
66
            }
67
            die('no valid item');
68
        }
69
    }
70
71 View Code Duplication
    public function downloadExample()
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...
72
    {
73
        $letter = $this->getModelFromRequest();
74
        $item = $letter->getItem();
75
76
        if ($letter->hasFile()) {
77
            $letter->downloadExample();
78
        }
79
80
        $this->flashRedirectLetter($item, $this->getModelManager()->getMessage('no-file'), 'error');
0 ignored issues
show
Bug introduced by
It seems like getMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
81
    }
82
83 View Code Duplication
    public function downloadBlank()
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...
84
    {
85
        $letter = $this->getModelFromRequest();
86
        $parent = $letter->getItem();
87
        if ($letter->hasFile()) {
88
            $letter->downloadBlank();
89
            die('');
90
        }
91
92
        $this->flashRedirectLetter($parent, $this->getModelManager()->getMessage('no-file'), 'error');
0 ignored issues
show
Bug introduced by
It seems like getMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
93
    }
94
95
    /**
96
     * @param Record $parent
97
     * @param $message
98
     * @param string $type
99
     */
100
    protected function flashRedirectLetter($parent, $message, $type = 'success')
101
    {
102
        $this->flashRedirect(
103
            $message,
104
            $this->getPdfLettersPageUrl($parent),
105
            $type,
106
            $this->getPdfLettersPageController($parent)
0 ignored issues
show
Documentation introduced by
$this->getPdfLettersPageController($parent) is of type null|string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
        );
108
    }
109
110
    /**
111
     * @param Record $parent
112
     * @return mixed
113
     */
114
    protected function getPdfLettersPageUrl($parent)
115
    {
116
        return $parent->getURL();
117
    }
118
119
    /**
120
     * @param Record $parent
121
     * @return mixed
122
     */
123
    protected function getPdfLettersPageController($parent)
124
    {
125
        return $parent->getManager()->getController();
126
    }
127
128
    /**
129
     * @return mixed
130
     */
131
    protected function viewCheckItem()
132
    {
133
        $letter = $this->getModelFromRequest();
134
135
        if (!$letter) {
136
            $this->redirect($this->getModelManager()->getUploadURL($_GET));
0 ignored issues
show
Bug introduced by
It seems like getUploadURL() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
137
        }
138
        return $letter;
139
    }
140
141
    /**
142
     * @param $item
143
     * @param $type
144
     * @return PdfLetterTrait|Record
145
     */
146
    protected function newPdfLetterRecordFromItemType($item, $type)
147
    {
148
        $letter = $this->newPdfLetterRecord();
149
        $letter->id_item = $item->id;
150
        $letter->type = $type;
151
        $letter->insert();
0 ignored issues
show
Bug introduced by
The method insert does only exist in Nip\Records\Record, but not in ByTIC\DocumentGenerator\PdfLetters\PdfLetterTrait.

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...
152
        return $letter;
153
    }
154
155
    /**
156
     * @return Record|PdfLetterTrait
157
     */
158
    protected function newPdfLetterRecord()
159
    {
160
        return $this->getModelManager()->getNew();
161
    }
162
163
    /**
164
     * @return RecordManager
165
     */
166
    protected function getParentManager()
167
    {
168
        return $this->parentManager;
169
    }
170
171
    /**
172
     * @return Record
173
     */
174
    protected function getParent()
175
    {
176
        return $this->parent;
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    protected function getLetterType()
183
    {
184
        return $this->letterType;
185
    }
186
187
    /**
188
     * Called before action
189
     */
190
    protected function parseRequestPdfLetter()
191
    {
192
        if ($this->getRequest()->get('id_item') && $this->getRequest()->get('type')) {
193
            $this->checkRequestForParent();
194
        } else {
195
            $this->checkRequestForItem();
196
        }
197
    }
198
199
    protected function checkRequestForParent()
200
    {
201
        $this->letterType = $this->getRequest()->get('type');
202
        $this->parentManager = $this->getModelManager()->getParentManagerFromType($this->getRequest()->get('type'));
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getModelManager()...Request()->get('type')) of type object<Nip\Records\Trait...ractTrait\RecordsTrait> is incompatible with the declared type object<Nip\Records\RecordManager> of property $parentManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
203
        $this->parent = $this->parentManager->findOne($this->getRequest()->get('id_item'));
0 ignored issues
show
Bug introduced by
The method findOne() does not exist on Nip\Records\Traits\AbstractTrait\RecordsTrait. Did you maybe mean findOneByQuery()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
204
        if ($this->parent) {
205
            $this->setModelFromRequest($this->getModelManager()->getByItem($this->letterType, $this->parent->id));
0 ignored issues
show
Bug introduced by
It seems like setModelFromRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
206
        }
207
    }
208
209
    protected function checkRequestForItem()
210
    {
211
        $letter = $this->getModelFromRequest();
212
        $this->checkRequestSetFromLetter($letter);
213
    }
214
215
    /**
216
     * @param PdfLetterTrait $letter
0 ignored issues
show
introduced by
The type PdfLetterTrait for parameter $letter is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
217
     */
218
    protected function checkRequestSetFromLetter($letter)
219
    {
220
        $this->letterType = $letter->type;
221
        $this->parent = $letter->getItem();
0 ignored issues
show
Documentation Bug introduced by
It seems like $letter->getItem() of type object<Nip\Records\Trait...tractTrait\RecordTrait> is incompatible with the declared type object<Nip\Records\Record> of property $parent.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
222
        $this->parentManager = $letter->getItemsManager();
0 ignored issues
show
Documentation Bug introduced by
It seems like $letter->getItemsManager() of type object<Nip\Records\Trait...tractTrait\RecordTrait> is incompatible with the declared type object<Nip\Records\RecordManager> of property $parentManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
223
    }
224
225
    /**
226
     * @inheritdoc
227
     */
228
    protected function setBreadcrumbs()
229
    {
230
        $this->call('setClassBreadcrumbs', $this->parentManager->getController());
0 ignored issues
show
Bug introduced by
It seems like call() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
231
        $this->call('setItemBreadcrumbs', $this->parentManager->getController(), false, [$this->parent]);
0 ignored issues
show
Bug introduced by
It seems like call() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
232
233
        $this->setClassBreadcrumbs();
0 ignored issues
show
Bug introduced by
It seems like setClassBreadcrumbs() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
234
    }
235
}
236