CloneLinks::isClone()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lincable\Eloquent;
4
5
use Lincable\MediaManager;
6
use Lincable\UrlGenerator;
7
8
/**
9
 * @method bool replicate()
10
 * @method string getUrlField()
11
 * @method mixed runQuiet()
12
 * @static \Lincable\MediaManager getMediaManager()
13
 * @static \Illuminate\Contracts\Events\Dispatcher getEventDispatcher()
14
 * @static \Illuminate\Contracts\Events\Dispatcher setEventDispatcher()
15
 * @static void unsetEventDispatcher()
16
 */
17
trait CloneLinks
18
{
19
    /**
20
     * The source model where this one was cloned from.
21
     * 
22
     * It's null when is not a clone.
23
     *
24
     * @var parent
0 ignored issues
show
Bug introduced by
The type Lincable\Eloquent\parent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
     */
26
    protected $sourceModel;
27
28
    /**
29
     * Boots the trait.
30
     * 
31
     * @return void
32
     */
33 31
    protected static function bootCloneLinks() 
34
    {
35
        static::created(function ($model) {
36 15
            static::copyCloneMedia($model);
37 31
        });
38 31
    }
39
40
    /**
41
     * Copy the media from source to the new cloned model. 
42
     *
43
     * @param  self  $model
44
     * @return void
45
     */
46 15
    protected static function copyCloneMedia(self $model)
47
    {
48 15
        if ($model->isClone()) {
49 2
            \Event::fakeFor(
50
                function () use ($model) {
51 2
                    static::getMediaManager()->copy(
52 2
                        $model->getSourceModel(), 
53 2
                        $model,
54 2
                        $model->preservesFilename()
55 2
                    )->save();
56 2
                }
57
            );
58
        }
59 15
    }
60
61
    /**
62
     *{@inheritDoc}
63
     */
64 3
    public function replicate(array $except = null)
65
    {
66 3
        $clone = parent::replicate($except);
67
        
68 3
        $clone->setSourceModel($this);
69
70 3
        return $clone;
71
    }
72
73
    /**
74
     * Set the newly source model.
75
     *
76
     * @param  parent  $model
77
     * @return void
78
     */
79 3
    public function setSourceModel(parent $model)
80
    {
81 3
        $this->sourceModel = $model;
82 3
    }
83
84
    /**
85
     * Return the model where this one was cloned.
86
     * 
87
     * @return parent|null
88
     */
89 2
    public function getSourceModel()
90
    {
91 2
        return $this->sourceModel;
92
    }
93
94
    /**
95
     * Defines wheter the class is an clone.
96
     * 
97
     * @return bool
98
     */
99 15
    public function isClone()
100
    {
101 15
        return $this->sourceModel !== null;
102
    }
103
104
    /**
105
     * Defines wheter the original filename should be preserved
106
     * when cloning the model.
107
     * 
108
     * @return bool
109
     */
110 2
    public function preservesFilename()
111
    {
112 2
        return $this->preserveName ?? false;
113
    }
114
}