PostRepository   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Test Coverage

Coverage 67.68%

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 178
ccs 44
cts 65
cp 0.6768
rs 10
c 0
b 0
f 0
wmc 20

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getById() 0 3 1
B getAll() 0 45 8
A assignDataAttributes() 0 27 4
A getBySlug() 0 3 1
A update() 0 16 3
A store() 0 14 2
A delete() 0 3 1
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Http\Requests\PostStoreRequest;
6
use App\Models\Post;
7
use Carbon\Carbon;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Config;
10
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
11
12
class PostRepository implements PostRepositoryInterface
13
{
14
15
    /**
16
     * Get all Posts.
17
     *
18
     * @param int|null $recordsPerPage
19
     * @param array|null $searchParameters
20
     *
21
     * @return \Illuminate\Support\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator
22
     */
23 1
    public function getAll(int $recordsPerPage = null, array $searchParameters = null)
24
    {
25 1
        $query = Post::orderBy('title', 'asc');
26
27 1
        if (!is_null($searchParameters)) {
28
            if (!empty($searchParameters['title'])) {
29
                $query->where(
30
                    'title',
31
                    'like',
32
                    '%' . $searchParameters['title'] . '%'
33
                );
34
            }
35
            if (!empty($searchParameters['categoryId'])) {
36
                $query->where('category_id', $searchParameters['categoryId']);
37
            }
38
            if (!empty($searchParameters['startDate'])) {
39
                $startDate = Carbon::createFromFormat(
40
                    'd/m/Y',
41
                    $searchParameters['startDate']
42
                );
43
                $query->where('created_at', '>=', $startDate);
44
            }
45
            if (!empty($searchParameters['endDate'])) {
46
                $endDate = Carbon::createFromFormat(
47
                    'd/m/Y',
48
                    $searchParameters['endDate']
49
                );
50
                $query->where('created_at', '<=', $endDate);
51
            }
52
            if (!empty($searchParameters['status'])) {
53
                $query->currentStatus($searchParameters['status']);
54
            }
55
        }
56
57 1
        // Avoid retrieving the post used for the static image gallery
58 1
        $query->where('title->en', '!=', 'Static pages images');
59
60
61
        if ($recordsPerPage) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $recordsPerPage of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
62
            $results = $query->paginate($recordsPerPage)->withQueryString();
63 1
        } else {
64
            $results = $query->get();
65
        }
66
67
        return $results;
68
    }
69
70
    /**
71
     * Get Post by id
72 2
     *
73
     * @param int $postId
74 2
     * @return Post
75
     */
76
    public function getById(int $postId): Post
77
    {
78
        return Post::findOrFail($postId);
79
    }
80
81
    /**
82
     * Get Post by slug
83
     *
84
     * @param string $postSlug
85 1
     *
86
     * @return Post
87 1
     */
88 1
    public function getBySlug(string $postSlug): Post
89
    {
90
        return Post::where('slug', $postSlug)->first();
91 1
    }
92
93 1
    /**
94
     * Store Post
95 1
     *
96 1
     * @param array $data
97
     *
98 1
     * @return Post
99
     * @throws \Spatie\ModelStatus\Exceptions\InvalidStatus
100
     */
101
    public function store(array $data): Post
102
    {
103
        $post = new Post();
104
        $post = self::assignDataAttributes($post, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\PostRep...:assignDataAttributes() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
        /** @scrutinizer ignore-call */ 
105
        $post = self::assignDataAttributes($post, $data);
Loading history...
105
106
        // Creator - Logged user id or 1 for factories
107
        $post->user_id = !is_null(Auth::id()) ? Auth::id() : 1;
0 ignored issues
show
Bug introduced by
The property user_id does not exist on App\Models\Post. Did you mean user?
Loading history...
108
109
        $post->save();
110 1
111
        $post->tags()->sync($data['tag_ids'] ?? null);
112 1
        $post->setStatus('published');
113 1
114
        return $post->fresh();
115 1
    }
116
117 1
    /**
118
     * Update Post
119
     *
120 1
     * @param array $data
121 1
     * @param int $id
122 1
     *
123
     * @return Post
124
     * @throws \Spatie\ModelStatus\Exceptions\InvalidStatus
125 1
     */
126
    public function update(array $data, int $id): Post
127
    {
128
        $post = $this->getById($id);
129
        $post = self::assignDataAttributes($post, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\PostRep...:assignDataAttributes() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
        /** @scrutinizer ignore-call */ 
130
        $post = self::assignDataAttributes($post, $data);
Loading history...
130
131
        $post->update();
132
133
        $post->tags()->sync($data['tag_ids'] ?? null);
134 1
135
        //$status = ($data['status'] == 'on') ? 'published' : 'unpublished';
136 1
        $status = (isset($data['status'])) ? 'published' : 'unpublished';
137 1
        if ($post->publishingStatus() != $status) {
138
            $post->setStatus($status);
139
        }
140
141
        return $post;
142
    }
143
144
    /**
145
     * Delete Post
146
     *
147 2
     * @param int $id
148
     * @return void
149 2
     */
150 2
    public function delete(int $id): void
151 2
    {
152
        Post::destroy($id);
153 2
    }
154 2
155 2
    /**
156
     * Assign the attributes of the data array to the object
157 2
     *
158 2
     * @param \App\Models\Post $post
159 2
     * @param array $data
160
     *
161 2
     * @return \App\Models\Post
162
     */
163
    public function assignDataAttributes(Post $post, array $data): Post
164
    {
165
        $post->title = $data['title'] ?? null;
166 2
        $post->category_id = $data['category_id'] ?? null;
0 ignored issues
show
Bug introduced by
The property category_id does not exist on App\Models\Post. Did you mean category?
Loading history...
167 2
        $post->intro_text = $data['intro_text'] ?? null;
168 2
169 2
        $post->body = $data['body'] ?? null;
170
        $post->before_content = $data['before_content'] ?? null;
171
        $post->after_content = $data['after_content'] ?? null;
172
173 2
        $post->featured = $data['featured'] ?? 0;
174
        $post->publish_at = $data['publish_at'] ?? null;
175
        $post->publish_until = $data['publish_until'] ?? null;
176
177
        if (isset($data['created_at'])) {
178
            $post->created_at = Carbon::createFromFormat('d/m/Y', $data['created_at']);
179
        }
180
181
        // Translations
182
        foreach (LaravelLocalization::getSupportedLocales() as $countryCode => $countryAvTrans) {
183
            if ($countryCode != Config::get('app.fallback_locale')) {
184
                $post->setTranslation('title', $countryCode, $data['title_' . $countryCode] ?? null);
185
                $post->setTranslation('body', $countryCode, $data['body_' . $countryCode] ?? null);
186
            }
187
        }
188
189
        return $post;
190
    }
191
}
192