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

ManagesProjects::purgeProject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 2
dl 0
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\Project;
6
7
trait ManagesProjects
8
{
9
    /**
10
     * Add a project to the portfolio.
11
     *
12
     * @param array $data Array of data to save.
13
     *
14
     * @return Project
15
     */
16 View Code Duplication
    public function addProject(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'] = Project::all()->pluck('order')->max() + 1;
19
20
        $project = Project::create($data);
21
22
        foreach (collect($data)->get('blocks', []) as $block) {
23
            $this->addBlockToModel($project, $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($project, $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 $project;
31
    }
32
33
    /**
34
     * Update a project in the portfolio.
35
     *
36
     * @param Project $project Project to update.
37
     * @param array   $data    Array of data to save.
38
     *
39
     * @return Project
40
     */
41
    public function updateProject(Project $project, array $data)
42
    {
43
        $project->update($data);
44
45
        $this->updateAllTextBlocks($project, $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($project, $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 $project;
50
    }
51
52
    /**
53
     * Remove a project from the portfolio.
54
     *
55
     * @param Project $project Project to remove.
56
     *
57
     * @return bool|null
58
     */
59
    public function removeProject(Project $project)
60
    {
61
        return $project->delete();
62
    }
63
64
    /**
65
     * Restore a soft deleted project.
66
     *
67
     * @param Project $project Project to restore.
68
     *
69
     * @return bool|null
70
     */
71
    public function restoreProject(Project $project)
72
    {
73
        $this->updateProject($project, ['visible' => false]);
74
75
        return $project->restore();
76
    }
77
78
    /**
79
     * Hard delete a project from the portfolio.
80
     *
81
     * @param Project $project Project to purge.
82
     *
83
     * @return bool|null
84
     */
85
    public function purgeProject(Project $project)
86
    {
87
        foreach ($project->images as $image) {
1 ignored issue
show
Documentation introduced by
The property images does not exist on object<Larafolio\Models\Project>. 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
        $project->restore();
92
93
        return $project->forceDelete();
94
    }
95
    
96
    /**
97
     * Update the order of projects in the portfolio.
98
     *
99
     * @param array $data Array of data for projects.
100
     */
101
    public function updateProjectOrder(array $data)
102
    {
103
        $projectData = $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...
104
105
        foreach ($projectData as $singleProjectData) {
106
            $project = Project::find($singleProjectData['id']);
107
108
            $project->update($singleProjectData);
109
        }
110
    }
111
}
112