Completed
Push — dev ( 1bdfc0...1b2c72 )
by Zach
03:26
created

ManagesPages::updatePageOrder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
nc 2
dl 10
loc 10
c 0
b 0
f 0
cc 2
eloc 5
nop 1
rs 9.4285
1
<?php
2
3
namespace Larafolio\Models\UserTraits;
4
5
use Larafolio\Models\Page;
6
7 View Code Duplication
trait ManagesPages
0 ignored issues
show
Duplication introduced by
This class 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...
8
{
9
    /**
10
     * Add a blocks and links to model.
11
     *
12
     * @param HasContent $model Model to add extras to.
13
     * @param array      $data  Array of posted user data.
14
     *
15
     * @return HasContent
16
     */
17
    protected abstract function addModelExtras(HasContent $model, array $data);
18
19
    /**
20
     * Update a HasContent model and its children.
21
     *
22
     * @param  HasContent $model Model to update.
23
     * @param  array      $data  Array of posted user data.
24
     *
25
     * @return HasContent
26
     */
27
    protected abstract function updateModel(HasContent $model, array $data);
28
29
    /**
30
     * Permanently delete a model.
31
     *
32
     * @param  HasContent $model Model to delete.
33
     *
34
     * @return boolean
35
     */
36
    protected abstract function purgeModel(HasContent $model);
37
38
    /**
39
     * Add a page to the portfolio.
40
     *
41
     * @param array $data Array of data to save.
42
     *
43
     * @return Project
44
     */
45
    public function addPage(array $data)
46
    {
47
        $data['order'] = Page::all()->pluck('order')->max() + 1;
48
49
        $page = Page::create($data);
50
51
        return $this->addModelExtras($page, $data);
0 ignored issues
show
Documentation introduced by
$page is of type object<Larafolio\Models\Page>, but the function expects a object<Larafolio\Models\UserTraits\HasContent>.

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...
52
    }
53
54
    /**
55
     * Update a page.
56
     *
57
     * @param Page  $page Page to update.
58
     * @param array $data Array of data to save.
59
     *
60
     * @return Page
61
     */
62
    public function updatePage(Page $page, array $data)
63
    {
64
        return $this->updateModel($page, $data);
0 ignored issues
show
Documentation introduced by
$page is of type object<Larafolio\Models\Page>, but the function expects a object<Larafolio\Models\UserTraits\HasContent>.

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...
65
    }
66
67
    /**
68
     * Remove a page.
69
     *
70
     * @param Page $page Page to remove.
71
     *
72
     * @return bool|null
73
     */
74
    public function removePage(Page $page)
75
    {
76
        return $page->delete();
77
    }
78
79
    /**
80
     * Restore a soft deleted page.
81
     *
82
     * @param Page $page Page to restore.
83
     *
84
     * @return bool|null
85
     */
86
    public function restorePage(Page $page)
87
    {
88
        $this->updatePage($page, ['visible' => false]);
89
90
        return $page->restore();
91
    }
92
93
    /**
94
     * Hard delete a page from the portfolio.
95
     *
96
     * @param Page $page Page to purge.
97
     *
98
     * @return bool|null
99
     */
100
    public function purgePage(Page $page)
101
    {
102
        return $this->purgeModel($page);
0 ignored issues
show
Documentation introduced by
$page is of type object<Larafolio\Models\Page>, but the function expects a object<Larafolio\Models\UserTraits\HasContent>.

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...
103
    }
104
    
105
    /**
106
     * Update the order of pages in the portfolio.
107
     *
108
     * @param array $data Array of data for pages.
109
     */
110
    public function updatePageOrder(array $data)
111
    {
112
        $pageData = $this->setOrder($data);
0 ignored issues
show
Bug introduced by
It seems like setOrder() 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...
113
114
        foreach ($pageData as $singlePageData) {
115
            $page = Page::find($singlePageData['id']);
116
117
            $page->update($singlePageData);
118
        }
119
    }
120
}
121