AccountScraper   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 35
c 3
b 2
f 0
dl 0
loc 63
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fetchOne() 0 22 1
A fetchLastPosts() 0 7 1
A fetchProfilePicIfNeeded() 0 18 2
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 11.01.2018
6
 */
7
8
namespace app\components\instagram;
9
10
11
use app\components\instagram\base\Scraper;
12
use app\components\instagram\contracts\AccountScraperInterface;
13
use app\components\instagram\models\Account as IgAccount;
14
use Jakim\Query\AccountQuery;
15
use Yii;
16
17
class AccountScraper extends Scraper implements AccountScraperInterface
18
{
19
    /**
20
     * @param string $ident username or id
21
     * @return \app\components\instagram\models\Account|null
22
     */
23
    public function fetchOne(string $ident): ?IgAccount
24
    {
25
        $query = new AccountQuery($this->httpClient);
26
        $account = $query->findOne($ident);
27
        $profilePic = $this->fetchProfilePicIfNeeded($account->username, $account->profilePicUrl);
28
29
        $model = new IgAccount();
30
        $model->id = $account->id;
31
        $model->username = $account->username;
32
        $model->profilePicUrl = $profilePic;
33
        $model->fullName = $account->fullName;
34
        $model->biography = $account->biography;
35
        $model->externalUrl = $account->externalUrl;
36
        $model->followedBy = $account->followedBy;
37
        $model->follows = $account->follows;
38
        $model->media = $account->media;
39
        $model->isPrivate = $account->isPrivate;
40
        $model->isVerified = $account->isVerified;
41
        $model->isBusiness = $account->isBusiness;
42
        $model->businessCategory = $account->businessCategory;
43
44
        return $model;
45
    }
46
47
    /**
48
     * @param string $username
49
     * @return \app\components\instagram\models\Post[]
50
     * @throws \Jakim\Exception\EmptyContentException
51
     * @throws \Jakim\Exception\RestrictedProfileException
52
     */
53
    public function fetchLastPosts(string $username): array
54
    {
55
        $query = new AccountQuery($this->httpClient);
56
        $posts = $query->findLastPosts($username, 10);
57
        $posts = $this->preparePosts($posts);
58
59
        return $posts;
60
    }
61
62
    private function fetchProfilePicIfNeeded($username, $profilePicUrl)
63
    {
64
        $username = strtolower($username);
65
        $filename = sprintf('%s_%s', $username, basename($profilePicUrl));
66
        $filename = explode('?', $filename)['0'];
67
        $path = sprintf('/uploads/%s', substr($username, 0, 2));
68
69
        $fullPath = Yii::getAlias("@app/web/{$path}");
70
        @mkdir($fullPath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

70
        /** @scrutinizer ignore-unhandled */ @mkdir($fullPath);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
71
        @chmod($fullPath, 0777);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for chmod(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

71
        /** @scrutinizer ignore-unhandled */ @chmod($fullPath, 0777);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
72
73
        $file = "{$fullPath}/{$filename}";
74
        if (!file_exists($file)) {
75
            $content = $this->httpClient->get($profilePicUrl)->getBody()->getContents();
76
            file_put_contents($file, $content);
77
        }
78
79
        return "{$path}/{$filename}";
80
    }
81
}