Completed
Push — master ( 58092b...0f4ccf )
by Ryan
05:18
created

StreamRepository   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 152
Duplicated Lines 9.21 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 14
loc 152
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B create() 14 24 3
A findBySlugAndNamespace() 0 4 1
A findAllBySearchable() 0 4 1
A findAllByNamespace() 0 4 1
A hidden() 0 4 1
A visible() 0 4 1
A destroy() 0 6 2
A cleanup() 0 21 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Anomaly\Streams\Platform\Stream;
2
3
use Anomaly\Streams\Platform\Model\EloquentCollection;
4
use Anomaly\Streams\Platform\Model\EloquentRepository;
5
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface;
6
use Anomaly\Streams\Platform\Stream\Contract\StreamRepositoryInterface;
7
use Illuminate\Database\Schema\Builder;
8
9
/**
10
 * Class StreamRepository
11
 *
12
 * @link    http://pyrocms.com/
13
 * @author  PyroCMS, Inc. <[email protected]>
14
 * @author  Ryan Thompson <[email protected]>
15
 */
16
class StreamRepository extends EloquentRepository implements StreamRepositoryInterface
17
{
18
19
    /**
20
     * The stream model.
21
     *
22
     * @var
23
     */
24
    protected $model;
25
26
    /**
27
     * The schema builder.
28
     *
29
     * @var Builder
30
     */
31
    protected $schema;
32
33
    /**
34
     * Create a new StreamRepository instance.
35
     *
36
     * @param StreamModel $model
37
     */
38
    public function __construct(StreamModel $model)
39
    {
40
        $this->model = $model;
41
42
        $this->schema = app('db')->connection()->getSchemaBuilder();
43
    }
44
45
    /**
46
     * Create a new Stream.
47
     *
48
     * @param  array $attributes
49
     * @return StreamInterface
50
     */
51
    public function create(array $attributes = [])
52
    {
53
        $attributes['config'] = array_get($attributes, 'config', []);
54
        $attributes['slug']   = str_slug(array_get($attributes, 'slug'), '_');
55
        $attributes['prefix'] = array_get($attributes, 'prefix', array_get($attributes, 'namespace') . '_');
56
57 View Code Duplication
        if (isset($attributes['name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
58
            array_set(
59
                $attributes,
60
                config('app.fallback_locale') . '.name',
61
                array_pull($attributes, 'name')
62
            );
63
        }
64
65 View Code Duplication
        if (isset($attributes['description'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66
            array_set(
67
                $attributes,
68
                config('app.fallback_locale') . '.description',
69
                array_pull($attributes, 'description')
70
            );
71
        }
72
73
        return $this->model->create($attributes);
74
    }
75
76
    /**
77
     * Find a stream by it's namespace and slug.
78
     *
79
     * @param  $slug
80
     * @param  $namespace
81
     * @return null|StreamInterface
82
     */
83
    public function findBySlugAndNamespace($slug, $namespace)
84
    {
85
        return $this->model->where('slug', $slug)->where('namespace', $namespace)->first();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
86
    }
87
88
    /**
89
     * Find all streams by their searchable flag.
90
     *
91
     * @param $searchable
92
     * @return StreamCollection
93
     */
94
    public function findAllBySearchable($searchable)
95
    {
96
        return $this->model->where('searchable', $searchable)->get();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
97
    }
98
99
    /**
100
     * Find all streams in a namespace.
101
     *
102
     * @param  $namespace
103
     * @return null|EloquentCollection
104
     */
105
    public function findAllByNamespace($namespace)
106
    {
107
        return $this->model->where('namespace', $namespace)->get();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
108
    }
109
110
    /**
111
     * Return streams that are/not hidden.
112
     *
113
     * @param $hidden
114
     * @return StreamCollection
115
     */
116
    public function hidden($hidden = true)
117
    {
118
        return $this->model->where('hidden', $hidden)->get();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
119
    }
120
121
    /**
122
     * Return only visible streams.
123
     *
124
     * @return StreamCollection
125
     */
126
    public function visible()
127
    {
128
        return $this->hidden(false);
129
    }
130
131
    /**
132
     * Destroy a namespace.
133
     *
134
     * @param $namespace
135
     */
136
    public function destroy($namespace)
137
    {
138
        foreach ($this->findAllByNamespace($namespace) as $stream) {
0 ignored issues
show
Bug introduced by
The expression $this->findAllByNamespace($namespace) of type null|object<Anomaly\Stre...del\EloquentCollection> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
139
            $this->delete($stream);
140
        }
141
    }
142
143
    /**
144
     * Clean up abandoned streams.
145
     */
146
    public function cleanup()
147
    {
148
        /* @var StreamInterface $stream */
149
        foreach ($this->model->all() as $stream) {
150
            if (!$this->schema->hasTable($stream->getEntryTableName())) {
151
                $this->delete($stream);
152
            }
153
        }
154
155
        $translations = $this->model->getTranslationModel();
156
157
        $translations
0 ignored issues
show
Documentation Bug introduced by
The method leftJoin does not exist on object<Anomaly\Streams\P...rm\Model\EloquentModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
158
            ->leftJoin(
159
                'streams_streams',
160
                'streams_streams_translations.stream_id',
161
                '=',
162
                'streams_streams.id'
163
            )
164
            ->whereNull('streams_streams.id')
165
            ->delete();
166
    }
167
}
168