Issues (76)

components/builders/MediaBuilder.php (1 issue)

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