Completed
Pull Request — master (#32)
by Sergey
08:58 queued 05:34
created

Bot   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A login() 0 7 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
    /**
29
     * @var ProvidersContainerInterface
30
     */
31
    private $providersContainer;
32
33
    public function __construct(ProvidersContainerInterface $providersContainer)
34
    {
35
        $this->providersContainer = $providersContainer;
36
37
        $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...
38
        $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...
39
    }
40
41
    /**
42
     * Login and parsing csrfToken from cookies if success
43
     * @param string $username
44
     * @param string $password
45
     * @return bool
46
     */
47
    public function login($username, $password)
48
    {
49
        $this->_checkCredentials($username, $password);
50
        $res = $this->pinners->login($username, $password);
51
52
        return $res;
53
    }
54
55
    /**
56
     * @param string $provider
57
     * @return Provider
58
     */
59
    public function __get($provider)
60
    {
61
        $provider = strtolower($provider);
62
63
        return $this->providersContainer->getProvider($provider);
64
    }
65
66
    /**
67
     * Validated password and login
68
     * @param string $username
69
     * @param string $password
70
     */
71
    protected function _checkCredentials($username, $password)
72
    {
73
        if (!$username || !$password) {
74
            throw new LogicException('You must set username and password to login.');
75
        }
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    public function getLastError()
82
    {
83
        return $this->providersContainer->getResponse()->getLastError();
84
    }
85
}