Completed
Push — master ( 0372a7...4c256e )
by Michael
01:10
created

src/Tasks/ImportInstagramPostsTask.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace X3dgoo\InstagramScraper\Tasks;
4
5
use InstagramScraper\Instagram;
6
use Phpfastcache\Helper\Psr16Adapter;
7
use SilverStripe\Core\Environment;
8
use SilverStripe\Dev\BuildTask;
9
use SilverStripe\ORM\DB;
10
use X3dgoo\InstagramScraper\Model\InstagramPost;
11
12
class ImportInstagramPostsTask extends BuildTask
13
{
14
    private static $segment = 'import-instagram-posts';
15
16
    protected $title = 'Import Instagram feed';
17
18
    protected $description = '';
19
20
    public function run($request)
21
    {
22
        $username = $request->getVar('username');
0 ignored issues
show
$username is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
23
        $username = Environment::getEnv('INSTAGRAM_USERNAME');
24
        $password = Environment::getEnv('INSTAGRAM_PASSWORD');
25
26
        if (!$username) {
27
            return;
28
        }
29
30
        $instagram = null;
31
32
        if ($username && $password) {
33
            $instagram = Instagram::withCredentials($username, $password, new Psr16Adapter('Files'));
34
            $instagram->login();
35
            $instagram->saveSession();
36
        } else {
37
            $instagram = new Instagram();
38
        }
39
40
        $instagramMedias = $instagram->getMedias($username);
41
42
        if (empty($instagramMedias)) {
43
            return;
44
        }
45
46
        foreach ($instagramMedias as $instagramMedia) {
47
            $instagramID = $instagramMedia->getId();
48
            $shortCode = $instagramMedia->getShortCode();
49
            $title = $instagramMedia->getCaption();
50
            $imageURL = $instagramMedia->getImageHighResolutionUrl();
51
            $imageThumbnailURL = $instagramMedia->getImageThumbnailUrl();
52
            $posted = $instagramMedia->getCreatedTime();
53
54
            $instagramPost = InstagramPost::get()->filter(['ShortCode' => $shortCode])->first();
55
56
            if (!$instagramPost || !$instagramPost->exists()) {
57
                $instagramPost = InstagramPost::create();
58
                $instagramPost->ShortCode = $shortCode;
59
            }
60
61
            $instagramPost->InstagramID = $instagramID;
62
            $instagramPost->Title = $title;
63
            $instagramPost->ImageURL = $imageURL;
64
            $instagramPost->ImageThumbnailURL = $imageThumbnailURL;
65
            $instagramPost->Posted = $posted;
66
            $instagramPost->write();
67
68
            DB::alteration_message('Imported instagram post ' . $shortCode);
69
        }
70
    }
71
}
72