Passed
Pull Request — main (#34)
by Tan
03:00
created

MetaBoxService::getCategoryMetaBox()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 1
c 2
b 1
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace CSlant\Blog\Api\Services;
4
5
use CSlant\Blog\Core\Constants\AppConstant;
0 ignored issues
show
Bug introduced by
The type CSlant\Blog\Core\Constants\AppConstant 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\Core\Facades\Base\MetaBox;
7
use CSlant\Blog\Core\Models\Category;
8
use CSlant\Blog\Core\Models\Page;
9
use CSlant\Blog\Core\Models\Post;
10
use CSlant\Blog\Core\Models\Tag;
11
use Illuminate\Database\Eloquent\Model;
12
13
class MetaBoxService
14
{
15
    protected function getSEOMetaBoxByModel(Model $model, string $lang = AppConstant::DEFAULT_LOCALE): ?Model
16
    {
17
        $metaKey = $lang === 'vi' ? 'seo_meta_vi' : 'seo_meta';
18
19
        return MetaBox::getMeta($model, $metaKey);
20
    }
21
22
    protected function getModelMetaBox(string $modelClass, int $modelId, string $lang = AppConstant::DEFAULT_LOCALE): ?Model
23
    {
24
        /** @var Model $modelClass */
25
        $model = $modelClass::query()->find($modelId);
26
27
        if (!$model) {
28
            return null;
29
        }
30
31
        return $this->getSEOMetaBoxByModel($model, $lang);
32
    }
33
34
    public function getPostMetaBox(int $modelId, mixed $lang): ?Model
35
    {
36
        return $this->getModelMetaBox(Post::getBaseModel(), $modelId, $lang);
37
    }
38
39
    public function getPageMetaBox(int $modelId, mixed $lang): ?Model
40
    {
41
        return $this->getModelMetaBox(Page::getBaseModel(), $modelId, $lang);
42
    }
43
44
    public function getCategoryMetaBox(int $modelId, mixed $lang): ?Model
45
    {
46
        return $this->getModelMetaBox(Category::getBaseModel(), $modelId, $lang);
47
    }
48
49
    public function getTagMetaBox(int $modelId, mixed $lang): ?Model
50
    {
51
        return $this->getModelMetaBox(Tag::getBaseModel(), $modelId, $lang);
52
    }
53
54
    /**
55
     * @param  string  $model
56
     * @param  int  $modelId
57
     * @param  string  $lang
58
     *
59
     * @return Model|null
60
     */
61
    public function getMetaBoxByModel(string $model, int $modelId, string $lang = AppConstant::DEFAULT_LOCALE): ?Model
62
    {
63
        if ($model === 'post') {
64
            return $this->getPostMetaBox($modelId, $lang);
65
        } elseif ($model === 'page') {
66
            return $this->getPageMetaBox($modelId, $lang);
67
        } elseif ($model === 'category') {
68
            return $this->getCategoryMetaBox($modelId, $lang);
69
        } elseif ($model === 'tag') {
70
            return $this->getTagMetaBox($modelId, $lang);
71
        } else {
72
            return null;
73
        }
74
    }
75
}
76