Passed
Push — master ( 347935...cc7747 )
by Mihail
05:42
created

ContentCategory::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 11
loc 11
rs 9.4285
1
<?php
2
3
namespace Apps\ActiveRecord;
4
5
use Ffcms\Core\App as MainApp;
6
use Ffcms\Core\Arch\ActiveModel;
7
use Ffcms\Core\Cache\MemoryObject;
8
use Ffcms\Core\Helper\Serialize;
9
use Ffcms\Core\Helper\Type\Str;
10
11
/**
12
 * Class ContentCategory. Active record model for content category nesting
13
 * @package Apps\ActiveRecord
14
 * @property int $id
15
 * @property string $path
16
 * @property string $title
17
 * @property string $description
18
 * @property string $configs
19
 * @property string $created_at
20
 * @property string $updated_at
21
 */
22
class ContentCategory extends ActiveModel
23
{
24
25
    /**
26
     * Get all table rows as object
27
     * @param array $columns
28
     * @return \Illuminate\Database\Eloquent\Collection|mixed|static[]
29
     */
30 View Code Duplication
    public static function all($columns = ['*'])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        $cacheName = 'activerecord.contentcategory.all.' . implode('.', $columns);
33
        $records = MemoryObject::instance()->get($cacheName);
34
        if ($records === null) {
35
            $records = parent::all($columns);
36
            MemoryObject::instance()->set($cacheName, $records);
37
        }
38
39
        return $records;
40
    }
41
    /**
42
     * Get record via category path address
43
     * @param string $path
44
     * @return self|object|null
45
     */
46
    public static function getByPath($path = '')
47
    {
48
        if (MainApp::$Memory->get('cache.content.category.path.' . $path) !== null) {
49
            return MainApp::$Memory->get('cache.content.category.path.' . $path);
50
        }
51
52
        $record = self::where('path', '=', $path)->first();
53
        MainApp::$Memory->set('cache.content.category.path.' . $path, $record);
54
        return $record;
55
    }
56
57
    /**
58
     * Find category by id
59
     * @param int $id
60
     * @return self|object|null
61
     */
62
    public static function getById($id)
63
    {
64
        if (MainApp::$Memory->get('cache.content.category.id.' . $id) !== null) {
65
            return MainApp::$Memory->get('cache.content.category.id.' . $id);
66
        }
67
68
        $record = self::find($id);
69
        MainApp::$Memory->set('cache.content.category.id.' . $id, $record);
70
        return $record;
71
    }
72
73
    /**
74
     * @deprecated
75
     * @return \Illuminate\Database\Eloquent\Collection|mixed|static[]
76
     */
77
    public static function getAll()
78
    {
79
        return self::all();
80
    }
81
82
    /**
83
     * Build id-title array of sorted by nesting level categories
84
     * @return array
85
     */
86
    public static function getSortedCategories()
87
    {
88
        $response = [];
89
        $tmpData = self::getSortedAll();
90
        foreach ($tmpData as $path => $data) {
91
            $title = null;
92
            if (Str::likeEmpty($path)) {
93
                $title .= '--';
94
            } else {
95
                // set level marker based on slashes count in pathway
96
                $slashCount = Str::entryCount($path, '/');
97
                for ($i=-1; $i <= $slashCount; $i++) {
98
                    $title .= '--';
99
                }
100
            }
101
            // add canonical title from db
102
            $title .= ' ' . Serialize::getDecodeLocale($data->title);
103
            // set response as array [id => title, ... ]
104
            $response[$data->id] = $title;
105
        }
106
107
        return $response;
108
    }
109
110
    /**
111
     * Get all categories sorted by pathway
112
     * @return array
113
     */
114
    public static function getSortedAll()
115
    {
116
        $list = self::all();
117
        $response = [];
118
        foreach ($list as $row) {
119
            $response[$row->path] = $row;
120
        }
121
        ksort($response);
122
        return $response;
123
    }
124
125
    /**
126
     * Get property by key of current category
127
     * @param string $key
128
     * @return bool|string|null
129
     */
130
    public function getProperty($key)
131
    {
132
        $properties = $this->configs;
133
        // check if properties is defined
134
        if (Str::likeEmpty($properties)) {
135
            return false;
136
        }
137
138
        $properties = Serialize::decode($properties);
139
        return $properties[$key];
140
    }
141
142
}