ManagesPortfolio::purgeProject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Larafolio\Models;
4
5
use Illuminate\Support\Facades\Storage;
6
7
trait ManagesPortfolio
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
    public function addProject(array $data)
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->addBlockToProject($project, $block);
24
        }
25
26
        foreach (collect($data)->get('links', []) as $link) {
27
            $this->addLinkToProject($project, $link);
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->updateProjectTextBlocks($project, $data);
46
47
        $this->updateProjectLinks($project, $data);
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);
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);
104
105
        foreach ($projectData as $singleProjectData) {
106
            $project = Project::find($singleProjectData['id']);
107
108
            $project->update($singleProjectData);
109
        }
110
    }
111
112
    /**
113
     * Set order of data based on order value.
114
     *
115
     * @param array $data Data array containing 'order' index.
116
     *
117
     * @return \Illuminate\Support\Collection
118
     */
119
    protected function setOrder(array $data)
120
    {
121
        return collect($data)->sortBy('order')
122
            ->map(function ($item, $key) {
123
                $item['order'] = $key;
124
125
                return $item;
126
            });
127
    }
128
129
    /**
130
     * Add a text block to a project.
131
     *
132
     * @param Project $project   Project to add text block to.
133
     * @param array   $blockData Array of text block data.
134
     *
135
     * @return \Illuminate\Database\Eloquent\Model
136
     */
137
    public function addBlockToProject(Project $project, array $blockData)
138
    {
139
        return $project->blocks()->create($blockData);
140
    }
141
142
    /**
143
     * Update a text block.
144
     *
145
     * @param TextBlock $textBlock Text block to update.
146
     * @param array     $blockData Array of text block data.
147
     *
148
     * @return TextBlock
149
     */
150
    public function updateTextBlock(TextBlock $textBlock, array $blockData)
151
    {
152
        $textBlock->update($blockData);
153
154
        return $textBlock;
155
    }
156
157
    /**
158
     * Update project text blocks by adding new ones and updating existing ones.
159
     *
160
     * @param Project $project Project that blocks belong to.
161
     * @param array   $data    Array of project information.
162
     */
163 View Code Duplication
    public function updateProjectTextBlocks(Project $project, 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...
164
    {
165
        $blockData = collect($data)->get('blocks', []);
166
167
        $blockData = $this->setOrder($blockData);
168
169
        foreach ($blockData as $singleBlockData) {
170
            if (isset($singleBlockData['project_id'])) {
171
                $block = TextBlock::find($singleBlockData['id']);
172
173
                $this->updateTextBlock($block, $singleBlockData);
174
            } else {
175
                $this->addBlockToProject($project, $singleBlockData);
176
            }
177
        }
178
    }
179
180
    /**
181
     * Remove a text block from a project.
182
     *
183
     * @param TextBlock $textBlock The text block to delete.
184
     *
185
     * @return bool|null
186
     */
187
    public function removeTextBlock(TextBlock $textBlock)
188
    {
189
        return $textBlock->delete();
190
    }
191
192
    /**
193
     * Add image to a project.
194
     *
195
     * @param Project $project   Project to add image to.
196
     * @param array   $imageData Array of image infomation.
197
     *
198
     * @return \Illuminate\Database\Eloquent\Model
199
     */
200
    public function addImageToProject(Project $project, array $imageData)
201
    {
202
        return $project->images()->create($imageData);
203
    }
204
205
    /**
206
     * Update image name and caption.
207
     *
208
     * @param Image $image     Image to update.
209
     * @param array $imageData Array of inmage information.
210
     *
211
     * @return Image
212
     */
213
    public function updateImageInfo(Image $image, array $imageData)
214
    {
215
        $image->update($imageData);
216
217
        return $image;
218
    }
219
220
    /**
221
     * Remove image from storage and delete database info.
222
     *
223
     * @param Image $image Image to remove.
224
     *
225
     * @return bool|null
226
     */
227
    public function removeImage(Image $image)
228
    {
229
        Storage::delete($image->path());
230
231
        return $image->delete();
232
    }
233
234
    /**
235
     * Add a link to a project.
236
     *
237
     * @param Project $project  Project to add link to.
238
     * @param array   $linkData Array of link info.
239
     */
240
    public function addLinkToProject(Project $project, array $linkData)
241
    {
242
        return $project->links()->create($linkData);
243
    }
244
245
    /**
246
     * Update a link.
247
     *
248
     * @param Link  $link     Link to update.
249
     * @param array $linkData Array of link data.
250
     *
251
     * @return Link
252
     */
253
    public function updateLink(Link $link, array $linkData)
254
    {
255
        $link->update($linkData);
256
257
        return $link;
258
    }
259
260
    /**
261
     * Update project links by adding new ones and updating existing ones.
262
     *
263
     * @param Project $project Project that links belong to.
264
     * @param array   $data    Array of project information.
265
     */
266 View Code Duplication
    public function updateProjectLinks(Project $project, 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...
267
    {
268
        $linkData = collect($data)->get('links', []);
269
270
        $linkData = $this->setOrder($linkData);
271
272
        foreach ($linkData as $singleLinkData) {
273
            if (isset($singleLinkData['project_id'])) {
274
                $link = Link::find($singleLinkData['id']);
275
276
                $this->updateLink($link, $singleLinkData);
277
            } else {
278
                $this->addLinkToProject($project, $singleLinkData);
279
            }
280
        }
281
    }
282
283
    /**
284
     * Remove link from a project.
285
     *
286
     * @param Link $link Link to remove.
287
     *
288
     * @return bool|null
289
     */
290
    public function removeLink(Link $link)
291
    {
292
        return $link->delete();
293
    }
294
}
295