Passed
Push — master ( b2168e...f1daef )
by Paweł
03:26
created

MediaUpdater::getNormalizedTakenAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 12.06.2018
6
 */
7
8
namespace app\components\updaters;
9
10
11
use app\components\instagram\models\Post;
12
use app\components\traits\SaveModelTrait;
13
use app\models\Media;
14
use yii\base\Component;
15
use yii\base\InvalidConfigException;
16
17
class MediaUpdater extends Component
18
{
19
    use SaveModelTrait;
0 ignored issues
show
Bug introduced by
The trait app\components\traits\SaveModelTrait requires the property $errors which is not provided by app\components\updaters\MediaUpdater.
Loading history...
20
21
    /**
22
     * @var \app\models\Media
23
     */
24
    public $media;
25
26
    public function init()
27
    {
28
        parent::init();
29
        if (!$this->media instanceof Media) {
0 ignored issues
show
introduced by
$this->media is always a sub-type of app\models\Media.
Loading history...
30
            throw new InvalidConfigException('Property \'media\' must be set and by type od \'\app\models\Media\'');
31
        }
32
    }
33
34
    public function setDetails(Post $post)
35
    {
36
        $this->media->instagram_id = $post->id;
37
        $this->media->shortcode = $post->shortcode;
38
        $this->media->is_video = $post->isVideo;
39
        $this->media->caption = $post->caption;
40
        $this->media->taken_at = $this->getNormalizedTakenAt($post);
41
        $this->media->likes = $post->likes;
42
        $this->media->comments = $post->comments;
43
44
        return $this;
45
    }
46
47
    public function save()
48
    {
49
        $this->saveModel($this->media);
50
    }
51
52
    protected function getNormalizedTakenAt(Post $post): string
53
    {
54
        return (new \DateTime('@' . $post->takenAt))->format('Y-m-d H:i:s');
55
    }
56
}