Passed
Pull Request — main (#34)
by Tan
02:54
created

MetaBoxController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 19
c 1
b 0
f 0
dl 0
loc 42
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMetaBoxBySlugModel() 0 9 2
A __construct() 0 4 1
A getMetaBoxByModel() 0 12 5
1
<?php
2
3
namespace CSlant\Blog\Api\Http\Controllers;
4
5
use CSlant\Blog\Api\Services\MetaBoxService;
6
use CSlant\Blog\Api\Services\SlugService;
7
use Illuminate\Database\Eloquent\Model;
8
9
class MetaBoxController
10
{
11
    protected MetaBoxService $metaBoxService;
12
13
    protected SlugService $slugService;
14
15
    public function __construct(MetaBoxService $metaBoxService, SlugService $slugService)
16
    {
17
        $this->metaBoxService = $metaBoxService;
18
        $this->slugService = $slugService;
19
    }
20
21
    public function getMetaBoxBySlugModel(string $model, string $slug, string $lang = 'en'): ?Model
22
    {
23
        $slugModel = $this->slugService->getSlugModel($slug, $model);
24
25
        if ($slugModel) {
26
            return $this->getMetaBoxByModel($model, $slugModel->reference_id, $lang);
27
        }
28
29
        return null;
30
    }
31
32
    /**
33
     * @param  string  $model
34
     * @param  int  $modelId
35
     * @param  string  $lang
36
     *
37
     * @return null|Model
38
     */
39
    public function getMetaBoxByModel(string $model, int $modelId, string $lang = 'en'): ?Model
40
    {
41
        if ($model === 'post') {
42
            return $this->metaBoxService->getPostMetaBox($modelId, $lang);
43
        } elseif ($model === 'page') {
44
            return $this->metaBoxService->getPageMetaBox($modelId, $lang);
45
        } elseif ($model === 'category') {
46
            return $this->metaBoxService->getCategoryMetaBox($modelId, $lang);
47
        } elseif ($model === 'tag') {
48
            return $this->metaBoxService->getTagMetaBox($modelId, $lang);
49
        } else {
50
            return null;
51
        }
52
    }
53
}
54