Completed
Pull Request — master (#32)
by Sergey
08:49
created

Bot   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 2
cbo 3
dl 0
loc 64
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A login() 0 10 1
A __get() 0 6 1
A _checkCredentials() 0 6 3
A getLastError() 0 4 1
1
<?php
2
3
namespace seregazhuk\PinterestBot;
4
5
use LogicException;
6
use seregazhuk\PinterestBot\Api\Providers\Pins;
7
use seregazhuk\PinterestBot\Api\Providers\Boards;
8
use seregazhuk\PinterestBot\Api\Providers\Pinners;
9
use seregazhuk\PinterestBot\Api\Providers\Provider;
10
use seregazhuk\PinterestBot\Api\Providers\Interests;
11
use seregazhuk\PinterestBot\Api\Providers\Conversations;
12
use seregazhuk\PinterestBot\Contracts\ProvidersContainerInterface;
13
14
/**
15
 * Class Bot
16
 *
17
 * @package Pinterest
18
 * @property string        $username
19
 * @property string        $password
20
 * @property Pinners       $pinners
21
 * @property Pins          $pins
22
 * @property Boards        $boards
23
 * @property Interests     $interests
24
 * @property Conversations $conversations
25
 */
26
class Bot
27
{
28
    protected $username;
29
    protected $password;
30
31
    /**
32
     * @var ProvidersContainerInterface
33
     */
34
    private $providersContainer;
35
36
    public function __construct(ProvidersContainerInterface $providersContainer)
37
    {
38
        $this->providersContainer = $providersContainer;
39
40
        $this->request = $providersContainer->getRequest();
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
        $this->response = $providersContainer->getResponse();
0 ignored issues
show
Bug introduced by
The property response does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
    }
43
44
    /**
45
     * Login and parsing csrfToken from cookies if success
46
     * @param $username
47
     * @param $password
48
     * @return bool
49
     */
50
    public function login($username, $password)
51
    {
52
        $this->username = $username;
53
        $this->password = $password;
54
55
        $this->_checkCredentials();
56
        $res = $this->pinners->login($this->username, $this->password);
57
58
        return $res;
59
    }
60
61
    /**
62
     * @param string $provider
63
     * @return Provider
64
     */
65
    public function __get($provider)
66
    {
67
        $provider = strtolower($provider);
68
69
        return $this->providersContainer->getProvider($provider);
70
    }
71
72
    /**
73
     * @throws LogicException
74
     */
75
    protected function _checkCredentials()
76
    {
77
        if ( ! $this->username || ! $this->password) {
78
            throw new LogicException('You must set username and password to login.');
79
        }
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    public function getLastError()
86
    {
87
        return $this->providersContainer->getResponse()->getLastError();
88
    }
89
}