Publication::setTypeId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Publication\Model;
4
5
use Application\Cache\Keys;
6
use Application\Mvc\Model\Model;
7
use Phalcon\Validation;
8
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;
9
use Application\Localization\Transliterator;
10
11
class Publication extends Model
12
{
13
14
    public function getSource()
15
    {
16
        return "publication";
17
    }
18
19
    public function initialize()
20
    {
21
        $this->hasMany('id', $this->translateModel, 'foreign_id'); // translate
22
23
        $this->belongsTo('type_id', 'Publication\Model\Type', 'id', [
24
            'alias' => 'type'
25
        ]);
26
    }
27
28
    private $id;
29
    private $type_id;
30
    private $slug;
31
    private $created_at;
32
    private $updated_at;
33
    private $date;
34
    private $preview_src;
35
    private $preview_inner;
36
    
37
    protected $title;
38
    protected $text;
39
    protected $meta_title;
40
    protected $meta_description;
41
    protected $meta_keywords;
42
43
    protected $translateModel = 'Publication\Model\Translate\PublicationTranslate'; // translate
44
    protected $translateFields = [
45
        'title',
46
        'meta_title',
47
        'meta_description',
48
        'meta_keywords',
49
        'text'
50
    ];
51
52
    public function beforeCreate()
53
    {
54
        $this->created_at = date("Y-m-d H:i:s");
55
    }
56
57
    public function beforeUpdate()
58
    {
59
        $this->updated_at = date("Y-m-d H:i:s");
60
    }
61
62
    public function afterUpdate()
63
    {
64
        parent::afterUpdate();
65
66
        $cache = $this->getDi()->get('cache');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDi() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
67
68
        $cache->delete(self::cacheSlugKey($this->getSlug()));
69
70
        $this->cacheManager->delete([
0 ignored issues
show
Documentation introduced by
The property cacheManager does not exist on object<Publication\Model\Publication>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
71
            Keys::PUBLICATION,
72
            $this->slug,
73
            self::$lang
74
        ]);
75
    }
76
77 View Code Duplication
    public function validation()
78
    {
79
        $validator = new Validation();
80
        $validator->add('slug', new UniquenessValidator(
81
            [
82
                "model"   => $this,
83
                "message" => $this->getDi()->get('helper')->translate("Publishcation with slug is already exists")
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDi() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
84
            ]
85
        ));
86
        return $this->validate($validator);
87
    }
88
89
    public function afterValidation()
90
    {
91
        if (!$this->date) {
92
            $this->date = date("Y-m-d H:i:s");
93
        }
94
    }
95
96
    public function updateFields($data)
97
    {
98
        if (!$this->getSlug()) {
99
            $this->setSlug(Transliterator::slugify($data['title']));
100
        }
101
        if (!$this->getMetaTitle()) {
102
            $this->setMetaTitle($data['title']);
103
        }
104
        $this->setPreviewInner(isset($data['preview_inner']) ? 1 : 0);
105
    }
106
107 View Code Duplication
    public static function findCachedBySlug($slug)
108
    {
109
        $publication = self::findFirst(["slug = '$slug'",
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $publication is correct as self::findFirst(array("s...g), 'lifetime' => 60))) (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
110
            'cache' => [
111
                'key'      => self::cacheSlugKey($slug),
112
                'lifetime' => 60]
113
        ]);
114
        return $publication;
115
    }
116
117
    public static function cacheSlugKey($slug)
118
    {
119
        $key = HOST_HASH . md5('Publication\Model\Publication; slug = ' . $slug);
120
        return $key;
121
    }
122
123
    public function setCreatedAt($created_at)
124
    {
125
        $this->created_at = $created_at;
126
        return $this;
127
    }
128
129
    public function getCreatedAt()
130
    {
131
        return $this->created_at;
132
    }
133
134
    public function setId($id)
135
    {
136
        $this->id = $id;
137
        return $this;
138
    }
139
140
    public function getId()
141
    {
142
        return $this->id;
143
    }
144
145
    public function setMetaDescription($meta_description)
146
    {
147
        $this->setMLVariable('meta_description', $meta_description);
148
        return $this;
149
    }
150
151
    public function getMetaDescription()
152
    {
153
        return $this->getMLVariable('meta_description');
154
    }
155
156
    public function setMetaKeywords($meta_keywords)
157
    {
158
        $this->setMLVariable('meta_keywords', $meta_keywords);
159
        return $this;
160
    }
161
162
    public function getMetaKeywords()
163
    {
164
        return $this->getMLVariable('meta_keywords');
165
    }
166
167
    public function setMetaTitle($meta_title)
168
    {
169
        $this->setMLVariable('meta_title', $meta_title);
170
        return $this;
171
    }
172
173
    public function getMetaTitle()
174
    {
175
        return $this->getMLVariable('meta_title');
176
    }
177
178
    public function setSlug($slug)
179
    {
180
        $this->slug = $slug;
181
        return $this;
182
    }
183
184
    public function getSlug()
185
    {
186
        return $this->slug;
187
    }
188
189
    public function setText($text)
190
    {
191
        $this->setMLVariable('text', $text);
192
        return $this;
193
    }
194
195
    public function getText()
196
    {
197
        return $this->getMLVariable('text');
198
    }
199
200
    public function setTitle($title)
201
    {
202
        $this->setMLVariable('title', $title);
203
        return $this;
204
    }
205
206
    public function getTitle()
207
    {
208
        return $this->getMLVariable('title');
209
    }
210
211
    public function setUpdatedAt($updated_at)
212
    {
213
        $this->updated_at = $updated_at;
214
        return $this;
215
    }
216
217
    public function getUpdatedAt()
218
    {
219
        return $this->updated_at;
220
    }
221
222
    public function setDate($date)
223
    {
224
        $this->date = $date;
225
        return $this;
226
    }
227
228
    public function getDate($format = 'Y-m-d H:i:s')
229
    {
230
        if ($format) {
231
            if ($this->date) {
232
                return date($format, strtotime($this->date));
233
            }
234
        } else {
235
            return $this->date;
236
        }
237
    }
238
239
    public function setTypeId($type_id)
240
    {
241
        $this->type_id = $type_id;
242
        return $this;
243
    }
244
245
    public function getTypeId()
246
    {
247
        return $this->type_id;
248
    }
249
250 View Code Duplication
    public function getTypeTitle()
251
    {
252
        if ($this->type_id) {
253
            $types = Type::cachedListArray(['key' => 'id']);
254
            if (array_key_exists($this->type_id, $types)) {
255
                return $types[$this->type_id];
256
            }
257
        }
258
    }
259
260 View Code Duplication
    public function getTypeSlug()
261
    {
262
        if ($this->type_id) {
263
            $types = Type::cachedListArray(['key' => 'id', 'value' => 'slug']);
264
            if (array_key_exists($this->type_id, $types)) {
265
                return $types[$this->type_id];
266
            }
267
        }
268
    }
269
270 View Code Duplication
    public function getTypeDisplayDate()
271
    {
272
        if ($this->type_id) {
273
            $types = Type::cachedListArray(['key' => 'id', 'value' => 'display_date']);
274
            if (array_key_exists($this->type_id, $types)) {
275
                return $types[$this->type_id];
276
            }
277
        }
278
    }
279
280
    public function setPreviewInner($preview_inner)
281
    {
282
        $this->preview_inner = $preview_inner;
283
        return $this;
284
    }
285
286
    public function getPreviewInner()
287
    {
288
        return $this->preview_inner;
289
    }
290
291
    public function getPreviewSrc()
292
    {
293
        return $this->preview_src;
294
    }
295
296
    public function setPreviewSrc($preview_src)
297
    {
298
        $this->preview_src = $preview_src;
299
        return $this;
300
    }
301
302
}
303