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

TagController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 3
eloc 24
c 2
b 1
f 0
dl 0
loc 40
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A findBySlug() 0 32 3
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
use CSlant\Blog\Core\Http\Responses\Base\BaseHttpResponse;
11
use CSlant\Blog\Core\Models\Slug;
12
13
/**
14
 * Class TagController
15
 *
16
 * @package CSlant\Blog\Api\Http\Controllers
17
 *
18
 * @group Blog
19
 *
20
 * @authenticated
21
 *
22
 * @method BaseHttpResponse httpResponse()
23
 * @method BaseHttpResponse setData(mixed $data)
24
 */
25
class TagController extends BaseTagController
26
{
27
    /**
28
     * Get tag by slug
29
     *
30
     * @group Blog
31
     * @queryParam slug Find by slug of tag.
32
     */
33
    public function findBySlug(string $slug)
34
    {
35
        /** @var Slug $slug */
36
        $slug = SlugHelper::getSlug($slug, SlugHelper::getPrefix(Tag::class));
37
        if (!$slug) {
0 ignored issues
show
introduced by
$slug is of type CSlant\Blog\Core\Models\Slug, thus it always evaluated to true.
Loading history...
38
            return $this
39
                ->httpResponse()
40
                ->setError()
41
                ->setCode(404)
42
                ->setMessage('Not found');
43
        }
44
45
        $tag = Tag::query()
46
            ->with('slugable')
47
            ->where([
48
                'id' => $slug->reference_id,
49
                'status' => StatusEnum::PUBLISHED,
50
            ])
51
            ->first();
52
53
        if (!$tag) {
54
            return $this
55
                ->httpResponse()
56
                ->setError()
57
                ->setCode(404)
58
                ->setMessage('Not found');
59
        }
60
61
        return $this
62
            ->httpResponse()
63
            ->setData(new TagResource($tag))
64
            ->toApiResponse();
65
    }
66
}
67