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'])) { |
|
|
|
|
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'])) { |
|
|
|
|
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(); |
|
|
|
|
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(); |
|
|
|
|
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(); |
|
|
|
|
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(); |
|
|
|
|
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) { |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
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.