Passed
Push — develop ( 28c299...928e2a )
by Mathias
12:40
created

ManageController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 17
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 10
cc 1
nc 1
nop 7
crap 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 controller */
11
namespace Auth\Controller;
12
13
use Auth\Adapter\HybridAuth;
14
use Auth\AuthenticationService;
15
use Auth\Form\SocialProfiles;
16
use Auth\Form\UserProfileContainer;
17
use Core\Repository\RepositoryService;
18
use Interop\Container\ContainerInterface;
19
use Zend\Mvc\Controller\AbstractActionController;
20
use Zend\Mvc\I18n\Translator;
21
use Zend\View\HelperPluginManager;
22
use Zend\View\Model\JsonModel;
23
use Core\Form\SummaryFormInterface;
24
25
/**
26
 * Main Action Controller for Authentication module.
27
 *
28
 */
29
class ManageController extends AbstractActionController
30
{
31
	private $userProfileContainer;
32
	
33
	private $authService;
34
	
35
	private $socialProfileForm;
36
	
37
	private $translator;
38
	
39
	private $repositories;
40
	
41
	private $viewHelper;
42
	
43
	private $hybridAuthAdapter;
44
	
45
	/**
46
	 * @param ContainerInterface $container
47
	 * @return ManageController
48
	 */
49 1
	static public function factory(ContainerInterface $container)
50
	{
51 1
		$forms = $container->get('forms');
52 1
		$userProfileContainer = $forms->get('Auth/UserProfileContainer');
53 1
		$socialProfileForm = $forms->get('Auth/SocialProfiles');
54 1
		$authService = $container->get('AuthenticationService');
55 1
		$translator = $container->get('translator');
56 1
		$repositories = $container->get('repositories');
57 1
		$viewHelper = $container->get('ViewHelperManager');
58 1
		$hybridAuthAdapter = $container->get('HybridAuthAdapter');
59 1
		$controller = new ManageController(
60 1
			$userProfileContainer,
61 1
			$authService,
62 1
			$repositories,
63 1
			$socialProfileForm,
64 1
			$translator,
65 1
			$viewHelper,
66 1
			$hybridAuthAdapter
67
		);
68 1
		return $controller;
69
	}
70
	
71 1
	public function __construct(
72
		UserProfileContainer $userProfileContainer,
73
		AuthenticationService $authService,
74
		RepositoryService $repositories,
75
		SocialProfiles $socialProfileForm,
76
		Translator $translator,
77
		HelperPluginManager $viewHelper,
78
		HybridAuth $hybridAuthAdapter
79
	)
80
	{
81 1
		$this->userProfileContainer = $userProfileContainer;
82 1
		$this->authService = $authService;
83 1
		$this->socialProfileForm = $socialProfileForm;
84 1
		$this->repositories = $repositories;
85 1
		$this->translator = $translator;
86 1
		$this->viewHelper = $viewHelper;
87 1
		$this->hybridAuthAdapter = $hybridAuthAdapter;
88 1
	}
89
	
90
	/**
91
     * @return array|JsonModel
92
     */
93 1
    public function profileAction()
94
    {
95
        /* @var \Auth\Form\UserProfileContainer $userProfileContainer */
96 1
        $userProfileContainer = $this->userProfileContainer;
97 1
        $user = $this->authService->getUser(); /* @var $user \Auth\Entity\User */
98 1
        $postProfiles = (array)$this->params()->fromPost('social_profiles');
99 1
        $userProfiles = $user->getProfile();
100 1
        $formSocialProfiles = $this->socialProfileForm
101 1
            ->setUseDefaultValidation(true)
102 1
            ->setData(['social_profiles' => array_map(function ($array)
103
            {
104
                return $array['data'];
105 1
            }, $userProfiles)]);
106
        
107 1
        $translator = $this->translator;
108
        /* @var \Auth\Form\SocialProfiles $formSocialProfiles */
109 1
        $formSocialProfiles->getBaseFieldset()
110 1
            ->setOption(
111 1
                'description',
112 1
                $translator->translate('You can connect your user profile with social networks. This allows you to log in via these networks.')
113
            );
114 1
        $userProfileContainer->setEntity($user);
115
116 1
        if ($this->request->isPost()) {
0 ignored issues
show
Bug introduced by
The method isPost() does not exist on Zend\Stdlib\RequestInterface. It seems like you code against a sub-type of Zend\Stdlib\RequestInterface such as Zend\Http\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        if ($this->request->/** @scrutinizer ignore-call */ isPost()) {
Loading history...
117
            $formName  = $this->params()->fromQuery('form');
118
            $form      = $userProfileContainer->getForm($formName);
119
            
120
            if ($form) {
121
                $postData  = $form->getOption('use_post_array') ? $_POST : array();
122
                //@TODO: [ZF3] option use_files_array is false by default
123
                //$filesData = $form->getOption('use_files_array') ? $_FILES : array();
124
	            $filesData = $_FILES;
125
                $data      = array_merge($postData, $filesData);
126
                $form->setData($data);
127
                
128
                if (!$form->isValid()) {
129
                    return new JsonModel(
130
                        array(
131
                        'valid' => false,
132
                        'errors' => $form->getMessages(),
133
                        )
134
                    );
135
                }
136
                
137
                $this->repositories->store($user);
138
                
139
                if ('file-uri' === $this->params()->fromPost('return')) {
140
                    $content = $form->getHydrator()->getLastUploadedFile()->getUri();
0 ignored issues
show
Bug introduced by
The method getHydrator() does not exist on Core\Form\Container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
                    $content = $form->/** @scrutinizer ignore-call */ getHydrator()->getLastUploadedFile()->getUri();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getLastUploadedFile() does not exist on Zend\Hydrator\HydratorInterface. It seems like you code against a sub-type of Zend\Hydrator\HydratorInterface such as Core\Entity\Hydrator\FileCollectionUploadHydrator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
                    $content = $form->getHydrator()->/** @scrutinizer ignore-call */ getLastUploadedFile()->getUri();
Loading history...
141
                } else {
142
                    if ($form instanceof SummaryFormInterface) {
143
                        $form->setRenderMode(SummaryFormInterface::RENDER_SUMMARY);
144
                        $viewHelper = 'summaryForm';
145
                    } else {
146
                        $viewHelper = 'form';
147
                    }
148
                    $content = $this->viewHelper->get($viewHelper)->__invoke($form);
149
                }
150
                
151
                return new JsonModel(
152
                    array(
153
                    'valid' => $form->isValid(),
154
                    'content' => $content,
155
                    )
156
                );
157
            }
158
            elseif ($postProfiles) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $postProfiles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
159
                $formSocialProfiles->setData($this->params()->fromPost());
160
                
161
                if ($formSocialProfiles->isValid()) {
162
                    $dataProfiles = $formSocialProfiles->getData()['social_profiles'];
163
                    $userRepository = $this->repositories->get('Auth/User'); /* @var $userRepository \Auth\Repository\User */
164
                    $hybridAuth = $this->hybridAuthAdapter->getHybridAuth();
165
                    
166
                    foreach ($dataProfiles as $network => $postProfile) {
167
                        // remove
168
                        if (isset($userProfiles[$network]) && !$dataProfiles[$network]) {
169
                            $user->removeProfile($network);
170
                        }
171
                        
172
                        // add
173
                        if (!isset($userProfiles[$network]) && $dataProfiles[$network]) {
174
                            $authProfile = $hybridAuth->authenticate($network)
175
                                ->getUserProfile();
176
                            // check for existing profiles
177
                            if ($userRepository->isProfileAssignedToAnotherUser($user->getId(), $authProfile->identifier, $network)) {
0 ignored issues
show
Bug introduced by
$user->getId() of type MongoId|string is incompatible with the type integer expected by parameter $curentUserId of Auth\Repository\User::is...AssignedToAnotherUser(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

177
                            if ($userRepository->isProfileAssignedToAnotherUser(/** @scrutinizer ignore-type */ $user->getId(), $authProfile->identifier, $network)) {
Loading history...
178
                                $dataProfiles[$network] = null;
179
                                $formSocialProfiles->setMessages(array(
180
                                    'social_profiles' => [
181
                                        $network => [sprintf($translator->translate('Could not connect your %s profile with your user account. The profile is already connected to another user account.'), $authProfile->displayName)]
182
                                    ]
183
                                ));
184
                            } else {
185
                                $profile = [
186
                                    'auth' => (array)$authProfile,
187
                                    'data' => \Zend\Json\Json::decode($dataProfiles[$network])
188
                                ];
189
                                $user->addProfile($network, $profile);
190
                            }
191
                        }
192
                    }
193
                }
194
                
195
                // keep data in sync & properly decoded
196
                $formSocialProfiles->setData(['social_profiles' => array_map(function ($array)
197
                {
198
                    return \Zend\Json\Json::decode($array) ?: '';
199
                }, $dataProfiles)]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dataProfiles does not seem to be defined for all execution paths leading up to this point.
Loading history...
200
            }
201
        }
202
        
203
        return array(
204
            'form' => $userProfileContainer,
205
            'socialProfilesForm' => $formSocialProfiles
206
        );
207
    }
208
}
209