Completed
Push — dev ( 2d24b0...5cd7c1 )
by Zach
06:05
created

ManagesPages::removePage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nop 1
rs 10
1
<?php
2
3
namespace Larafolio\Models\UserTraits;
4
5
use Larafolio\Models\Page;
6
7
trait ManagesPages
8
{
9
    /**
10
     * Add a page to the portfolio.
11
     *
12
     * @param array $data Array of data to save.
13
     *
14
     * @return Project
15
     */
16 View Code Duplication
    public function addPage(array $data)
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...
17
    {
18
        $data['order'] = Page::all()->pluck('order')->max() + 1;
19
20
        $page = Page::create($data);
21
22
        foreach (collect($data)->get('blocks', []) as $block) {
23
            $this->addBlockToModel($page, $block);
0 ignored issues
show
Bug introduced by
It seems like addBlockToModel() 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...
24
        }
25
26
        foreach (collect($data)->get('links', []) as $link) {
27
            $this->addLinkToModel($page, $link);
0 ignored issues
show
Bug introduced by
It seems like addLinkToModel() 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...
28
        }
29
30
        return $page;
31
    }
32
33
    /**
34
     * Update a page.
35
     *
36
     * @param Page  $page Page to update.
37
     * @param array $data Array of data to save.
38
     *
39
     * @return Page
40
     */
41
    public function updatePage(Page $page, array $data)
42
    {
43
        $page->update($data);
44
45
        $this->updateAllTextBlocks($page, $data);
0 ignored issues
show
Bug introduced by
It seems like updateAllTextBlocks() 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...
46
47
        $this->updateAllLinks($page, $data);
0 ignored issues
show
Bug introduced by
It seems like updateAllLinks() 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...
48
49
        return $page;
50
    }
51
52
    /**
53
     * Remove a page.
54
     *
55
     * @param Page $page Page to remove.
56
     *
57
     * @return bool|null
58
     */
59
    public function removePage(Page $page)
60
    {
61
        return $page->delete();
62
    }
63
64
    /**
65
     * Restore a soft deleted page.
66
     *
67
     * @param Page $page Page to restore.
68
     *
69
     * @return bool|null
70
     */
71
    public function restorePage(Page $page)
72
    {
73
        $this->updatePage($page, ['visible' => false]);
74
75
        return $page->restore();
76
    }
77
78
    /**
79
     * Hard delete a page from the portfolio.
80
     *
81
     * @param Page $page Page to purge.
82
     *
83
     * @return bool|null
84
     */
85
    public function purgePage(Page $page)
86
    {
87
        foreach ($page->images as $image) {
1 ignored issue
show
Documentation introduced by
The property images does not exist on object<Larafolio\Models\Page>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
88
            $this->removeImage($image);
0 ignored issues
show
Bug introduced by
It seems like removeImage() 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...
89
        }
90
91
        $page->restore();
92
93
        return $page->forceDelete();
94
    }
95
}
96