Completed
Push — master ( ce82f5...154931 )
by Iurii
54s
created

Base::setProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/**
4
 * @package Social Login
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2015, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3
8
 */
9
10
namespace gplcart\modules\social_login\handlers;
11
12
use gplcart\core\Container;
13
14
/**
15
 * Base class for other Oauth 2.0 providers
16
 */
17
class Base
18
{
19
20
    /**
21
     * Socket client helper instance
22
     * @var \gplcart\core\helpers\SocketClient $socket
23
     */
24
    protected $socket;
25
26
    /**
27
     * User model instance
28
     * @var \gplcart\core\models\User $user
29
     */
30
    protected $user;
31
32
    /**
33
     * Store model instance
34
     * @var \gplcart\core\models\Store $store
35
     */
36
    protected $store;
37
38
    /**
39
     * Constructor
40
     */
41
    public function __construct()
42
    {
43
        $this->user = Container::get('gplcart\\core\\models\\User');
44
        $this->store = Container::get('gplcart\\core\\models\\Store');
45
        $this->socket = Container::get('gplcart\\core\\helpers\\SocketClient');
46
    }
47
48
    /**
49
     * Sets a property
50
     * @param string $property
51
     * @param mixed $value
52
     */
53
    public function setProperty($property, $value)
54
    {
55
        $this->{$property} = $value;
56
    }
57
58
    /**
59
     * Login/register a new user
60
     * @param array $user
61
     * @param array $provider
62
     * @return mixed
63
     */
64
    protected function submitUser(array $user, array $provider)
65
    {
66
        $existing = $this->user->getByEmail($user['email']);
67
68
        if (empty($existing)) {
69
            return $this->registerUser($user, $provider);
70
        }
71
72
        return $this->user->login($existing, false);
73
    }
74
75
    /**
76
     * Register a new user
77
     * @param array $user
78
     * @param array $provider
79
     * @return mixed
80
     */
81
    protected function registerUser(array $user, array $provider)
82
    {
83
        if (empty($provider['settings']['register'])) {
84
            return false;
85
        }
86
87
        $store = $this->store->getCurrent();
88
89
        $user['store_id'] = $store['store_id'];
90
        $user['password'] = $this->user->generatePassword();
91
        $user['login'] = !empty($provider['settings']['register_login']);
92
        $user['status'] = !empty($provider['settings']['register_status']);
93
94
        return $this->user->register($user);
95
    }
96
97
    /**
98
     * Request user data
99
     * @param array $params
100
     * @param string $url
101
     * @param array $query
102
     * @return mixed
103
     */
104
    protected function requestData(array $params, $url, $query = array())
105
    {
106
        try {
107
            $query += array('access_token' => $params['token']);
108
            $response = $this->socket->request($url, array('query' => array_filter($query)));
109
            return json_decode($response['data'], true);
110
        } catch (\Exception $ex) {
111
            trigger_error($ex->getMessage());
112
            return array();
113
        }
114
    }
115
116
}
117