Completed
Push — develop ( 7597eb...6d38ef )
by
unknown
9s
created

HybridAuth::setSocialProfilePlugin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 * @author Carsten Bleek <[email protected]>
9
 * @author Mathias Gelhausen <[email protected]>
10
 * @author Miroslav Fedeleš <[email protected]>
11
 */
12
13
/** Auth adapter */
14
namespace Auth\Adapter;
15
16
use Hybrid_Auth;
17
use Zend\Authentication\Result;
18
use Zend\Authentication\Adapter\AdapterInterface;
19
use Doctrine\MongoDB\GridFSFile;
20
use Auth\Entity\UserImage;
21
use Auth\Controller\Plugin\SocialProfiles as SocialProfilePlugin;
22
23
/**
24
 * This class allows to authenticate with HybridAuth
25
 *
26
 * HybridAuth adapter for \Zend\Authentication
27
 *
28
 * Class HybridAuth
29
 * @package Auth\Adapter
30
 */
31
class HybridAuth implements AdapterInterface
0 ignored issues
show
Coding Style introduced by
The property $_hybridAuth is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $_provider is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
32
{
33
    /**
34
     * HybridAuth instance.
35
     *
36
     * @var Hybrid_Auth
37
     */
38
    protected $_hybridAuth;
0 ignored issues
show
Coding Style introduced by
$_hybridAuth does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
39
40
    /**
41
     * User mapper.
42
     *
43
     * @var \Auth\Repository\User
44
     */
45
    protected $repository;
46
    
47
    /**
48
     * HybridAuth provider identifier
49
     *
50
     * @var string
51
     */
52
    protected $_provider;
0 ignored issues
show
Coding Style introduced by
$_provider does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
53
    
54
    /**
55
     * Social profile plugin
56
     *
57
     * @var SocialProfilePlugin
58
     */
59
    protected $socialProfilePlugin;
60
    
61
    /**
62
     * Sets the provider identifier used by HybridAuth.
63
     *
64
     * @param string $provider
65
     * @return HybridAuth
66
     */
67
    public function setProvider($provider)
68
    {
69
        $this->_provider = $provider;
70
        return $this;
71
    }
72
    
73
    /**
74
     * Gets the provider identifier used by HybridAuth.
75
     *
76
     * @return string|null
77
     */
78
    public function getProvider()
79
    {
80
        return $this->_provider;
81
    }
82
    
83
    /**
84
     * {@inheritdoc}
85
     *
86
     *
87
     * @see \Zend\Authentication\Adapter\AdapterInterface::authenticate()
88
     */
89
    public function authenticate()
90
    {
91
        $hybridAuth = $this->getHybridAuth();
92
        /* @var $adapter \Hybrid_Provider_Model */
93
        $adapter = $hybridAuth->authenticate($this->_provider);
94
95
        $userProfile = $adapter->getUserProfile();
96
        $email = isset($userProfile->emailVerified) && !empty($userProfile->emailVerified)
97
              ? $userProfile->emailVerified
98
              : $userProfile->email;
99
       
100
       
101
        $forceSave = false;
102
        $user = $this->getRepository()->findByProfileIdentifier($userProfile->identifier, $this->_provider, ['allowDeactivated' => true]);
103
104
        if (!$user) {
105
            $forceSave = true;
106
            $user = $this->getRepository()->create();
107
        }
108
       
109
       
110
        $currentInfo = $user->getProfile($this->_provider);
0 ignored issues
show
Unused Code introduced by
The call to UserInterface::getProfile() has too many arguments starting with $this->_provider.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
The method getProfile does only exist in Auth\Entity\UserInterface, but not in Core\Entity\EntityInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
111
        
112
        try {
113
            $socialData = $this->socialProfilePlugin->fetch($this->_provider)->getData();
114
        } catch (\InvalidArgumentException $e) {
115
            // social profile adapter does not exist
116
            $socialData = [];
117
        }
118
        
119
        $newInfo = [
120
            'auth' => (array) $userProfile,
121
            'data' => $socialData,
122
        ];
123
       
124
        if ($forceSave || $currentInfo != $newInfo) {
125
            /*  */
126
127
            $dm = $this->getRepository()->getDocumentManager();
128
            if ('' == $user->getInfo()->email) {
129
                $user->getInfo()->email = $email;
0 ignored issues
show
Bug introduced by
The method getInfo does only exist in Auth\Entity\UserInterface, but not in Core\Entity\EntityInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
130
            }
131
            $user->getInfo()->firstName = $userProfile->firstName;
132
            $user->getInfo()->lastName = $userProfile->lastName;
133
            $user->getInfo()->birthDay = $userProfile->birthDay;
134
            $user->getInfo()->birthMonth = $userProfile->birthMonth;
135
            $user->getInfo()->birthYear = $userProfile->birthYear;
136
            $user->getInfo()->postalcode = $userProfile->zip;
137
            $user->getInfo()->city = $userProfile->city;
138
            $user->getInfo()->street = $userProfile->address;
139
            $user->getInfo()->phone = $userProfile->phone;
140
            $user->getInfo()->gender = $userProfile->gender;
141
            
142
            // $user->setLogin($email); // this may cause duplicate key exception
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
143
            $user->addProfile($this->_provider, $newInfo);
144
145
            $dm->persist($user);
146
            // make sure all ids are generated and user exists in database.
147
            $dm->flush();
148
149
            /*
150
            * This must be after flush because a newly created user has no id!
151
            */
152
            if ($forceSave || (!$user->getInfo()->image && $userProfile->photoURL)) {
153
                // get user image
154
                if ('' != $userProfile->photoURL) {
155
                    $client = new \Zend\Http\Client($userProfile->photoURL, array('sslverifypeer' => false));
156
                    $response = $client->send();
157
                    $file = new GridFSFile();
158
                    $file->setBytes($response->getBody());
159
160
                    $userImage = new UserImage();
161
                    $userImage->setName($userProfile->lastName.$userProfile->firstName);
162
                    $userImage->setType($response->getHeaders()->get('Content-Type')->getFieldValue());
0 ignored issues
show
Bug introduced by
The method getFieldValue does only exist in Zend\Http\Header\HeaderInterface, but not in ArrayIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
163
                    $userImage->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type object<Core\Entity\EntityInterface>; however, Core\Entity\FileEntity::setUser() does only seem to accept object<Auth\Entity\UserInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
164
                    $userImage->setFile($file);
165
                    $user->getInfo()->setImage($userImage);
166
                    $dm->persist($userImage);
167
                    //$this->getRepository()->store($user->info);
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
168
                }
169
170
                // We have to flush again
171
                $dm->flush();
172
            }
173
        }
174
        
175
       
176
        return new Result(Result::SUCCESS, $user->getId(), array('firstLogin' => $forceSave, 'user' => $user));
0 ignored issues
show
Bug introduced by
The method getId does only exist in Auth\Entity\UserInterface, but not in Core\Entity\EntityInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
177
    }
178
179
    /**
180
     * Get the Hybrid_Auth object
181
     *
182
     * @return Hybrid_Auth
183
     */
184
    public function getHybridAuth()
185
    {
186
        return $this->_hybridAuth;
187
    }
188
189
    /**
190
     * Set the Hybrid_Auth object
191
     *
192
     * @param  Hybrid_Auth    $hybridAuth
193
     * @return HybridAuth
194
     */
195
    public function setHybridAuth(Hybrid_Auth $hybridAuth)
196
    {
197
        $this->_hybridAuth = $hybridAuth;
198
199
        return $this;
200
    }
201
202
    /**
203
     * Sets the user repository
204
     *
205
     * @param $repository
206
     *
207
     * @return $this
208
     */
209
    public function setRepository($repository)
210
    {
211
        $this->repository = $repository;
212
213
        return $this;
214
    }
215
216
    /**
217
     * Gets the user repository
218
     *
219
     * @return \Auth\Repository\User
220
     */
221
    public function getRepository()
222
    {
223
        return $this->repository;
224
    }
225
	/**
226
	 * @param SocialProfilePlugin
227
	 * @return HybridAuth
228
	 */
229
	public function setSocialProfilePlugin(SocialProfilePlugin $socialProfilePlugin)
230
	{
231
		$this->socialProfilePlugin = $socialProfilePlugin;
232
		
233
		return $this;
234
	}
235
}
236