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

AdminPdfLetterFieldsControllerTrait::addNewModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 9
cp 0
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace ByTIC\DocumentGenerator\PdfLetters\Controllers\Fields;
4
5
use ByTIC\DocumentGenerator\PdfLetters\PdfLetterTrait;
6
use Nip\Controllers\Traits\AbstractControllerTrait;
7
use Nip\Records\AbstractModels\Record;
8
9
/**
10
 * Trait AdminPdfLetterFieldsControllerTrait
11
 * @package ByTIC\DocumentGenerator\PdfLetters
12
 */
13
trait AdminPdfLetterFieldsControllerTrait
14
{
15
    use AbstractControllerTrait;
16
17
    /**
18
     * @var PdfLetterTrait
19
     */
20
    protected $pdfLetter;
21
22
    /**
23
     * @var Record
24
     */
25
    protected $parent;
26
27
    /**
28
     * @return PdfLetterTrait
0 ignored issues
show
Comprehensibility Bug introduced by
The return type PdfLetterTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
29
     */
30
    public function addNewModel()
31
    {
32
        /** @var PdfLetterTrait $item */
33
        $item = $this->getModelManager()->getNew();
34
        if ($this->pdfLetter) {
35
            $item->populateFromLetter($this->pdfLetter);
0 ignored issues
show
Bug introduced by
It seems like populateFromLetter() 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...
36
            $this->getView()->Breadcrumbs()->addItem(
37
                $this->getModelManager()->getLetterManager()->getLabel('add'),
38
                '#'
39
            );
40
            return $item;
41
        }
42
43
        return $this->forward('index', 'error');
0 ignored issues
show
Bug introduced by
It seems like forward() 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...
44
    }
45
46
    /**
47
     * Called before action
48
     */
49
    protected function parseRequestPdfLetterField()
50
    {
51
        if ($this->getRequest()->get('id_letter')) {
52
            $this->pdfLetter = $this->checkForeignModelFromRequest(
53
                $this->getModelManager()->getLetterManager()->getTable(),
54
                'id_letter'
0 ignored issues
show
Documentation introduced by
'id_letter' is of type 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...
55
            );
56
        } else {
57
            $field = $this->getModelFromRequest();
58
            $this->pdfLetter = $field->getPdfLetter();
59
        }
60
61
        $this->parent = $this->pdfLetter->getItem();
62
    }
63
64
    /**
65
     * @param bool $key
66
     * @return FieldTrait|Record
67
     */
68
    abstract protected function getModelFromRequest($key = false);
69
70
    /**
71
     * @param $name
72
     * @param bool $key
73
     * @return mixed
74
     */
75
    abstract protected function checkForeignModelFromRequest($name, $key = false);
76
}