Completed
Push — develop ( bffb2a...35cb48 )
by
unknown
07:30
created

HybridAuth::setRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
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
 */
9
10
/** Auth adapter */
11
namespace Auth\Adapter;
12
13
use Hybrid_Auth;
14
use Zend\Authentication\Result;
15
use Zend\Authentication\Adapter\AdapterInterface;
16
use Doctrine\MongoDB\GridFSFile;
17
use Auth\Entity\UserImage;
18
use Auth\Controller\Plugin\SocialProfiles as SocialProfilePlugin;
19
20
/**
21
 * This class allows to authenticate with HybridAuth
22
 *
23
 * HybridAuth adapter for \Zend\Authentication
24
 *
25
 * Class HybridAuth
26
 * @package Auth\Adapter
27
 */
28
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...
29
{
30
    /**
31
     * HybridAuth instance.
32
     *
33
     * @var Hybrid_Auth
34
     */
35
    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...
36
37
    /**
38
     * User mapper.
39
     *
40
     * @var \Auth\Repository\User
41
     */
42
    protected $repository;
43
    
44
    /**
45
     * HybridAuth provider identifier
46
     *
47
     * @var string
48
     */
49
    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...
50
    
51
    /**
52
     * Social profile plugin
53
     *
54
     * @var SocialProfilePlugin
55
     */
56
    protected $socialProfilePlugin;
57
    
58
    /**
59
     * Sets the provider identifier used by HybridAuth.
60
     *
61
     * @param string $provider
62
     * @return HybridAuth
63
     */
64
    public function setProvider($provider)
65
    {
66
        $this->_provider = $provider;
67
        return $this;
68
    }
69
    
70
    /**
71
     * Gets the provider identifier used by HybridAuth.
72
     *
73
     * @return string|null
74
     */
75
    public function getProvider()
76
    {
77
        return $this->_provider;
78
    }
79
    
80
    /**
81
     * {@inheritdoc}
82
     *
83
     *
84
     * @see \Zend\Authentication\Adapter\AdapterInterface::authenticate()
85
     */
86
    public function authenticate()
87
    {
88
        $hybridAuth = $this->getHybridAuth();
89
        /* @var $adapter \Hybrid_Provider_Model */
90
        $adapter = $hybridAuth->authenticate($this->_provider);
91
92
        $userProfile = $adapter->getUserProfile();
93
        $email = isset($userProfile->emailVerified) && !empty($userProfile->emailVerified)
94
              ? $userProfile->emailVerified
95
              : $userProfile->email;
96
       
97
       
98
        $forceSave = false;
99
        $user = $this->getRepository()->findByProfileIdentifier($userProfile->identifier, $this->_provider);
100
101
        if (!$user) {
102
            $forceSave = true;
103
            $user = $this->getRepository()->create();
104
        }
105
       
106
       
107
        $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...
108
        $newInfo = [
109
            'auth' => (array) $userProfile,
110
            'data' => $this->socialProfilePlugin->fetch($this->_provider)->getData(),
111
        ];
112
       
113
        if ($forceSave || $currentInfo != $newInfo) {
114
            /*  */
115
116
            $dm = $this->getRepository()->getDocumentManager();
117
            if ('' == $user->getInfo()->email) {
118
                $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...
119
            }
120
            $user->getInfo()->firstName = $userProfile->firstName;
121
            $user->getInfo()->lastName = $userProfile->lastName;
122
            $user->getInfo()->birthDay = $userProfile->birthDay;
123
            $user->getInfo()->birthMonth = $userProfile->birthMonth;
124
            $user->getInfo()->birthYear = $userProfile->birthYear;
125
            $user->getInfo()->postalcode = $userProfile->zip;
126
            $user->getInfo()->city = $userProfile->city;
127
            $user->getInfo()->street = $userProfile->address;
128
            $user->getInfo()->phone = $userProfile->phone;
129
            $user->getInfo()->gender = $userProfile->gender;
130
            
131
            // $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...
132
            $user->addProfile($this->_provider, $newInfo);
133
134
            $dm->persist($user);
135
            // make sure all ids are generated and user exists in database.
136
            $dm->flush();
137
138
            /*
139
            * This must be after flush because a newly created user has no id!
140
            */
141
            if ($forceSave || (!$user->getInfo()->image && $userProfile->photoURL)) {
142
                // get user image
143
                if ('' != $userProfile->photoURL) {
144
                    $client = new \Zend\Http\Client($userProfile->photoURL, array('sslverifypeer' => false));
145
                    $response = $client->send();
146
                    $file = new GridFSFile();
147
                    $file->setBytes($response->getBody());
148
149
                    $userImage = new UserImage();
150
                    $userImage->setName($userProfile->lastName.$userProfile->firstName);
151
                    $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...
152
                    $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...
153
                    $userImage->setFile($file);
154
                    $user->getInfo()->setImage($userImage);
155
                    $dm->persist($userImage);
156
                    //$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...
157
                }
158
159
                // We have to flush again
160
                $dm->flush();
161
            }
162
        }
163
       
164
       
165
        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...
166
    }
167
168
    /**
169
     * Get the Hybrid_Auth object
170
     *
171
     * @return Hybrid_Auth
172
     */
173
    public function getHybridAuth()
174
    {
175
        return $this->_hybridAuth;
176
    }
177
178
    /**
179
     * Set the Hybrid_Auth object
180
     *
181
     * @param  Hybrid_Auth    $hybridAuth
182
     * @return HybridAuth
183
     */
184
    public function setHybridAuth(Hybrid_Auth $hybridAuth)
185
    {
186
        $this->_hybridAuth = $hybridAuth;
187
188
        return $this;
189
    }
190
191
    /**
192
     * Sets the user repository
193
     *
194
     * @param $repository
195
     *
196
     * @return $this
197
     */
198
    public function setRepository($repository)
199
    {
200
        $this->repository = $repository;
201
202
        return $this;
203
    }
204
205
    /**
206
     * Gets the user repository
207
     *
208
     * @return \Auth\Repository\User
209
     */
210
    public function getRepository()
211
    {
212
        return $this->repository;
213
    }
214
	/**
215
	 * @param SocialProfilePlugin
216
	 * @return HybridAuth
217
	 */
218
	public function setSocialProfilePlugin(SocialProfilePlugin $socialProfilePlugin)
219
	{
220
		$this->socialProfilePlugin = $socialProfilePlugin;
221
		
222
		return $this;
223
	}
224
}
225