Completed
Push — master ( 2d0d9b...2037bb )
by Sergey
05:22 queued 02:31
created

multiple_accounts_and_proxy.php ➔ getImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 36 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
require __DIR__ . '/vendor/autoload.php';
4
5
use seregazhuk\PinterestBot\Factories\PinterestBot;
6
7
$accounts = [
8
    [
9
        'login' => 'mylogin1',
10
        'password' => 'mypass1',
11
        'username' => 'cats_account',
12
        'images' => 'images/cats_pics',
13
        'link' => 'http://awasome-blog-about-cats.com',
14
        'proxy' => [
15
            'host' => '123.123.21.21',
16
            'post' => 1234
17
        ],
18
    ],
19
    [
20
        'login' => 'mylogin2',
21
        'password' => 'mypass2',
22
        'username' => 'dogs_account',
23
        'images' => 'images/dogs_pics',
24
        'link' => 'http://awasome-blog-about-dogs.com',
25
        'proxy' => [
26
            'host' => '123.123.22.22',
27
            'post' => 5678
28
        ]
29
    ]
30
];
31
32
/**
33
 * @param string $folder
34
 * @return string
35
 */
36
function getImage($folder) {
37
38
    $images = glob("$folder/*.*");
39
    if(empty($images)) {
40
        echo "No images for posting\n";
41
        die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function getImage() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
42
    }
43
44
    return $images[0];
45
}
46
47
$bot = PinterestBot::create();
48
49
foreach($accounts as $account) {
50
    $bot->auth->login($account['login'], $account['password']);
51
52
    // add proxy
53
    if(isset($account['proxy'])) {
54
        $proxy = $account['proxy'];
55
        $bot->getHttpClient()->useProxy($proxy['host'], $proxy['port']);
0 ignored issues
show
Documentation Bug introduced by
The method getHttpClient does not exist on object<seregazhuk\PinterestBot\Bot>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
56
    }
57
58
    if ($bot->user->isBanned()) {
59
        $username = $account['username'];
60
        die("Account $username has been banned!\n");
61
    }
62
63
    // get board id
64
    $boards = $bot->boards->forUser($account['username']);
65
    $boardId = $boards[0]['id'];
66
67
    // select image for posting
68
    $image = getImage($account['images']);
69
70
    // select keyword
71
    $keywords = $account['keywords'];
72
    $keyword = $keywords[array_rand($keywords)];
73
74
    // create a pin
75
    $bot->pins->create($image, $boardId, $keyword, $account['link']);
76
77
    // remove image
78
    unlink($image);
79
    $bot->auth->logout();
80
}