Passed
Pull Request — main (#32)
by
unknown
02:21
created

TagController::findBySlug()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 31
rs 9.552
cc 3
nc 3
nop 1
1
<?php
2
3
namespace CSlant\Blog\Api\Http\Controllers;
4
5
use Botble\Blog\Models\Tag;
0 ignored issues
show
Bug introduced by
The type Botble\Blog\Models\Tag 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...
6
use CSlant\Blog\Api\Enums\StatusEnum;
7
use CSlant\Blog\Api\Http\Resources\TagResource;
8
use CSlant\Blog\Core\Facades\Base\SlugHelper;
9
use CSlant\Blog\Core\Http\Controllers\Base\BaseTagController;
10
11
class TagController extends BaseTagController
12
{
13
    /**
14
     * Get tag by slug
15
     *
16
     * @group Blog
17
     * @queryParam slug Find by slug of tag.
18
     */
19
    public function findBySlug(string $slug)
20
    {
21
        $slug = SlugHelper::getSlug($slug, SlugHelper::getPrefix(Tag::class));
22
        if (!$slug) {
23
            return $this
24
                ->httpResponse()
25
                ->setError()
26
                ->setCode(404)
27
                ->setMessage('Not found');
28
        }
29
30
        $tag = Tag::query()
31
            ->with('slugable')
32
            ->where([
33
                'id' => $slug->reference_id,
34
                'status' => StatusEnum::PUBLISHED,
35
            ])
36
            ->first();
37
38
        if (!$tag) {
39
            return $this
40
                ->httpResponse()
41
                ->setError()
42
                ->setCode(404)
43
                ->setMessage('Not found');
44
        }
45
46
        return $this
47
            ->httpResponse()
48
            ->setData(new TagResource($tag))
49
            ->toApiResponse();
50
    }
51
}
52