Skip to content

Commit d375707

Browse files
committed
Show result JSON only on failure, otherwise redirect to route 'zfcuser'.
1 parent 77a6a13 commit d375707

3 files changed

Lines changed: 26 additions & 17 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
],
1919
"require": {
2020
"php": "^5.5|^7.0",
21-
"zf-commons/zfc-user": "^3.0"
21+
"zf-commons/zfc-user": "^3.0",
22+
"phpoption/phpoption": "^1.0"
2223
},
2324
"autoload": {
2425
"psr-4": {

src/Controller/Substitute.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ public function __construct(SubstituteService $substituteService) {
1818
}
1919

2020
public function substituteAction() {
21-
$result = $this->substituteService->substitute($this->params('user'));
22-
return new JsonModel($result);
21+
$errorOption = $this->substituteService->substitute($this->params('user'));
22+
foreach ($errorOption as $error) {
23+
return new JsonModel(['error' => $error]);
24+
}
25+
return $this->redirect()->toRoute('zfcuser');
2326
}
2427

2528
public function unsubstituteAction() {
26-
$result = $this->substituteService->unsubstitute();
27-
return new JsonModel($result);
29+
$errorOption = $this->substituteService->unsubstitute();
30+
foreach ($errorOption as $error) {
31+
return new JsonModel(['error' => $error]);
32+
}
33+
return $this->redirect()->toRoute('zfcuser');
2834
}
2935

3036
}

src/Service/Substitute.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Zend\Authentication\Storage\StorageInterface;
66
use Zend\Authentication\AuthenticationService;
77
use ZfcUser\Mapper\User as UserMapper;
8+
use PhpOption\Option as PhpOption;
89

910
class Substitute {
1011

@@ -32,38 +33,39 @@ public function __construct(
3233
$this->setStorage($storage);
3334
}
3435

36+
/**
37+
* @param string $userId
38+
* @return PhpOption error reason
39+
*/
3540
public function substitute($userId) {
36-
$error['result'] = 'error';
37-
$success['result'] = 'success';
38-
3941
$identity = $this->getAuthService()->getIdentity();
4042
if (!$identity) {
41-
return $error + ['message' => 'Not logged in'];
43+
return PhpOption::ensure('Not logged in');
4244
}
4345
if (!$this->getStorage()->isEmpty()) {
44-
return $error + ['message' => 'Already substituted'];
46+
return PhpOption::ensure('Already substituted');
4547
}
4648
if (!$this->getUserMapper()->findById($userId)) {
47-
return $error + ['message' => 'Substitution user does not exist'];
49+
return PhpOption::ensure('Substitution user does not exist');
4850
}
4951

5052
$this->getStorage()->write($identity);
5153
$this->getAuthService()->getStorage()->write($userId);
52-
return $success;
54+
return PhpOption::ensure(null);
5355
}
5456

57+
/**
58+
* @return PhpOption error reason
59+
*/
5560
public function unsubstitute() {
56-
$error['result'] = 'error';
57-
$success['result'] = 'success';
58-
5961
if ($this->getStorage()->isEmpty()) {
60-
return $error + ['message' => 'Not substituted'];
62+
return PhpOption::ensure('Not substituted in the first place');
6163
}
6264
$originalUser = $this->getStorage()->read();
6365
$this->getStorage()->clear();
6466

6567
$this->getAuthService()->getStorage()->write($originalUser->getId());
66-
return $success;
68+
return PhpOption::ensure(null);
6769
}
6870

6971
public function isSubstituted() {

0 commit comments

Comments
 (0)