|
5 | 5 | use Zend\Authentication\Storage\StorageInterface; |
6 | 6 | use Zend\Authentication\AuthenticationService; |
7 | 7 | use ZfcUser\Mapper\User as UserMapper; |
| 8 | +use PhpOption\Option as PhpOption; |
8 | 9 |
|
9 | 10 | class Substitute { |
10 | 11 |
|
@@ -32,38 +33,39 @@ public function __construct( |
32 | 33 | $this->setStorage($storage); |
33 | 34 | } |
34 | 35 |
|
| 36 | + /** |
| 37 | + * @param string $userId |
| 38 | + * @return PhpOption error reason |
| 39 | + */ |
35 | 40 | public function substitute($userId) { |
36 | | - $error['result'] = 'error'; |
37 | | - $success['result'] = 'success'; |
38 | | - |
39 | 41 | $identity = $this->getAuthService()->getIdentity(); |
40 | 42 | if (!$identity) { |
41 | | - return $error + ['message' => 'Not logged in']; |
| 43 | + return PhpOption::ensure('Not logged in'); |
42 | 44 | } |
43 | 45 | if (!$this->getStorage()->isEmpty()) { |
44 | | - return $error + ['message' => 'Already substituted']; |
| 46 | + return PhpOption::ensure('Already substituted'); |
45 | 47 | } |
46 | 48 | if (!$this->getUserMapper()->findById($userId)) { |
47 | | - return $error + ['message' => 'Substitution user does not exist']; |
| 49 | + return PhpOption::ensure('Substitution user does not exist'); |
48 | 50 | } |
49 | 51 |
|
50 | 52 | $this->getStorage()->write($identity); |
51 | 53 | $this->getAuthService()->getStorage()->write($userId); |
52 | | - return $success; |
| 54 | + return PhpOption::ensure(null); |
53 | 55 | } |
54 | 56 |
|
| 57 | + /** |
| 58 | + * @return PhpOption error reason |
| 59 | + */ |
55 | 60 | public function unsubstitute() { |
56 | | - $error['result'] = 'error'; |
57 | | - $success['result'] = 'success'; |
58 | | - |
59 | 61 | if ($this->getStorage()->isEmpty()) { |
60 | | - return $error + ['message' => 'Not substituted']; |
| 62 | + return PhpOption::ensure('Not substituted in the first place'); |
61 | 63 | } |
62 | 64 | $originalUser = $this->getStorage()->read(); |
63 | 65 | $this->getStorage()->clear(); |
64 | 66 |
|
65 | 67 | $this->getAuthService()->getStorage()->write($originalUser->getId()); |
66 | | - return $success; |
| 68 | + return PhpOption::ensure(null); |
67 | 69 | } |
68 | 70 |
|
69 | 71 | public function isSubstituted() { |
|
0 commit comments