@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | |
29 | 29 | public function do(GuardInterface $guard) |
30 | 30 | { |
31 | - if (!$guard->allows('do')) { |
|
31 | + if (!$guard->allows('do')){ |
|
32 | 32 | throw new ControllerException("Unauthorized permission 'do'", ControllerException::FORBIDDEN); |
33 | 33 | } |
34 | 34 | |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | |
38 | 38 | public function token(AuthContextInterface $authContext) |
39 | 39 | { |
40 | - if ($authContext->getToken() !== null) { |
|
40 | + if ($authContext->getToken() !== null){ |
|
41 | 41 | return $authContext->getToken()->getID(); |
42 | 42 | } |
43 | 43 | |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | |
56 | 56 | public function token2() |
57 | 57 | { |
58 | - if ($this->auth->getToken() !== null) { |
|
58 | + if ($this->auth->getToken() !== null){ |
|
59 | 59 | return $this->auth->getToken()->getID(); |
60 | 60 | } |
61 | 61 |
@@ -26,10 +26,12 @@ discard block |
||
26 | 26 | $this->auth = $auth; |
27 | 27 | } |
28 | 28 | |
29 | - public function do(GuardInterface $guard) |
|
29 | + public function do{ |
|
30 | + (GuardInterface $guard) |
|
30 | 31 | { |
31 | 32 | if (!$guard->allows('do')) { |
32 | 33 | throw new ControllerException("Unauthorized permission 'do'", ControllerException::FORBIDDEN); |
34 | + } |
|
33 | 35 | } |
34 | 36 | |
35 | 37 | return 'ok'; |
@@ -37,7 +39,8 @@ discard block |
||
37 | 39 | |
38 | 40 | public function token(AuthContextInterface $authContext) |
39 | 41 | { |
40 | - if ($authContext->getToken() !== null) { |
|
42 | + if ($authContext->getToken() !== null) |
|
43 | + { |
|
41 | 44 | return $authContext->getToken()->getID(); |
42 | 45 | } |
43 | 46 | |
@@ -55,7 +58,8 @@ discard block |
||
55 | 58 | |
56 | 59 | public function token2() |
57 | 60 | { |
58 | - if ($this->auth->getToken() !== null) { |
|
61 | + if ($this->auth->getToken() !== null) |
|
62 | + { |
|
59 | 63 | return $this->auth->getToken()->getID(); |
60 | 64 | } |
61 | 65 |
@@ -40,8 +40,10 @@ |
||
40 | 40 | * @Guarded("do") |
41 | 41 | * @return string |
42 | 42 | */ |
43 | - public function do() |
|
43 | + public function do{ |
|
44 | + () |
|
44 | 45 | { |
45 | 46 | return 'ok'; |
46 | 47 | } |
48 | + } |
|
47 | 49 | } |
@@ -40,22 +40,22 @@ discard block |
||
40 | 40 | */ |
41 | 41 | public function load(string $id): ?TokenInterface |
42 | 42 | { |
43 | - try { |
|
43 | + try{ |
|
44 | 44 | $tokenData = $this->session->getSection(self::SESSION_SECTION)->get('token'); |
45 | - if ($tokenData === null) { |
|
45 | + if ($tokenData === null){ |
|
46 | 46 | return null; |
47 | 47 | } |
48 | 48 | |
49 | 49 | $token = Token::unpack($tokenData); |
50 | - } catch (\Throwable $e) { |
|
50 | + }catch (\Throwable $e){ |
|
51 | 51 | throw new TokenStorageException('Unable to load session token', $e->getCode(), $e); |
52 | 52 | } |
53 | 53 | |
54 | - if (!hash_equals($token->getID(), $id)) { |
|
54 | + if (!hash_equals($token->getID(), $id)){ |
|
55 | 55 | return null; |
56 | 56 | } |
57 | 57 | |
58 | - if ($token->getExpiresAt() !== null && $token->getExpiresAt() > new \DateTime()) { |
|
58 | + if ($token->getExpiresAt() !== null && $token->getExpiresAt() > new \DateTime()){ |
|
59 | 59 | $this->delete($token); |
60 | 60 | return null; |
61 | 61 | } |
@@ -68,12 +68,12 @@ discard block |
||
68 | 68 | */ |
69 | 69 | public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface |
70 | 70 | { |
71 | - try { |
|
71 | + try{ |
|
72 | 72 | $token = new Token($this->randomHash(128), $payload, $expiresAt); |
73 | 73 | $this->session->getSection(self::SESSION_SECTION)->set('token', $token->pack()); |
74 | 74 | |
75 | 75 | return $token; |
76 | - } catch (\Throwable $e) { |
|
76 | + }catch (\Throwable $e){ |
|
77 | 77 | throw new TokenStorageException('Unable to create session token', $e->getCode(), $e); |
78 | 78 | } |
79 | 79 | } |
@@ -40,22 +40,28 @@ discard block |
||
40 | 40 | */ |
41 | 41 | public function load(string $id): ?TokenInterface |
42 | 42 | { |
43 | - try { |
|
43 | + try |
|
44 | + { |
|
44 | 45 | $tokenData = $this->session->getSection(self::SESSION_SECTION)->get('token'); |
45 | - if ($tokenData === null) { |
|
46 | + if ($tokenData === null) |
|
47 | + { |
|
46 | 48 | return null; |
47 | 49 | } |
48 | 50 | |
49 | 51 | $token = Token::unpack($tokenData); |
50 | - } catch (\Throwable $e) { |
|
52 | + } |
|
53 | + catch (\Throwable $e) |
|
54 | + { |
|
51 | 55 | throw new TokenStorageException('Unable to load session token', $e->getCode(), $e); |
52 | 56 | } |
53 | 57 | |
54 | - if (!hash_equals($token->getID(), $id)) { |
|
58 | + if (!hash_equals($token->getID(), $id)) |
|
59 | + { |
|
55 | 60 | return null; |
56 | 61 | } |
57 | 62 | |
58 | - if ($token->getExpiresAt() !== null && $token->getExpiresAt() > new \DateTime()) { |
|
63 | + if ($token->getExpiresAt() !== null && $token->getExpiresAt() > new \DateTime()) |
|
64 | + { |
|
59 | 65 | $this->delete($token); |
60 | 66 | return null; |
61 | 67 | } |
@@ -68,12 +74,15 @@ discard block |
||
68 | 74 | */ |
69 | 75 | public function create(array $payload, \DateTimeInterface $expiresAt = null): TokenInterface |
70 | 76 | { |
71 | - try { |
|
77 | + try |
|
78 | + { |
|
72 | 79 | $token = new Token($this->randomHash(128), $payload, $expiresAt); |
73 | 80 | $this->session->getSection(self::SESSION_SECTION)->set('token', $token->pack()); |
74 | 81 | |
75 | 82 | return $token; |
76 | - } catch (\Throwable $e) { |
|
83 | + } |
|
84 | + catch (\Throwable $e) |
|
85 | + { |
|
77 | 86 | throw new TokenStorageException('Unable to create session token', $e->getCode(), $e); |
78 | 87 | } |
79 | 88 | } |
@@ -45,22 +45,22 @@ discard block |
||
45 | 45 | { |
46 | 46 | $entities = $this->getDeclaredEntities($controller, $action); |
47 | 47 | |
48 | - foreach ($entities as $parameter => $role) { |
|
48 | + foreach ($entities as $parameter => $role){ |
|
49 | 49 | $value = $this->getParameter($parameter, $parameters, count($entities) === 1); |
50 | - if ($value === null) { |
|
50 | + if ($value === null){ |
|
51 | 51 | throw new ControllerException( |
52 | 52 | "Entity `{$parameter}` can not be found", |
53 | 53 | ControllerException::NOT_FOUND |
54 | 54 | ); |
55 | 55 | } |
56 | 56 | |
57 | - if (is_object($value)) { |
|
57 | + if (is_object($value)){ |
|
58 | 58 | // pre-filled |
59 | 59 | continue; |
60 | 60 | } |
61 | 61 | |
62 | 62 | $entity = $this->resolveEntity($role, $value); |
63 | - if ($entity === null) { |
|
63 | + if ($entity === null){ |
|
64 | 64 | throw new ControllerException( |
65 | 65 | "Entity `{$parameter}` can not be found", |
66 | 66 | ControllerException::NOT_FOUND |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | */ |
82 | 82 | protected function getParameter(string $role, array $parameters, bool $useDefault = false) |
83 | 83 | { |
84 | - if (!$useDefault) { |
|
84 | + if (!$useDefault){ |
|
85 | 85 | return $parameters[$role] ?? null; |
86 | 86 | } |
87 | 87 | |
@@ -106,23 +106,23 @@ discard block |
||
106 | 106 | private function getDeclaredEntities(string $controller, string $action): array |
107 | 107 | { |
108 | 108 | $key = sprintf('%s:%s', $controller, $action); |
109 | - if (array_key_exists($key, $this->cache)) { |
|
109 | + if (array_key_exists($key, $this->cache)){ |
|
110 | 110 | return $this->cache[$key]; |
111 | 111 | } |
112 | 112 | |
113 | 113 | $this->cache[$key] = []; |
114 | - try { |
|
114 | + try{ |
|
115 | 115 | $method = new \ReflectionMethod($controller, $action); |
116 | - } catch (\ReflectionException $e) { |
|
116 | + }catch (\ReflectionException $e){ |
|
117 | 117 | return []; |
118 | 118 | } |
119 | 119 | |
120 | - foreach ($method->getParameters() as $parameter) { |
|
121 | - if ($parameter->getClass() === null) { |
|
120 | + foreach ($method->getParameters() as $parameter){ |
|
121 | + if ($parameter->getClass() === null){ |
|
122 | 122 | continue; |
123 | 123 | } |
124 | 124 | |
125 | - if ($this->orm->getSchema()->defines($parameter->getClass()->getName())) { |
|
125 | + if ($this->orm->getSchema()->defines($parameter->getClass()->getName())){ |
|
126 | 126 | $this->cache[$key][$parameter->getName()] = $this->orm->resolveRole($parameter->getClass()->getName()); |
127 | 127 | } |
128 | 128 | } |
@@ -45,22 +45,26 @@ discard block |
||
45 | 45 | { |
46 | 46 | $entities = $this->getDeclaredEntities($controller, $action); |
47 | 47 | |
48 | - foreach ($entities as $parameter => $role) { |
|
48 | + foreach ($entities as $parameter => $role) |
|
49 | + { |
|
49 | 50 | $value = $this->getParameter($parameter, $parameters, count($entities) === 1); |
50 | - if ($value === null) { |
|
51 | + if ($value === null) |
|
52 | + { |
|
51 | 53 | throw new ControllerException( |
52 | 54 | "Entity `{$parameter}` can not be found", |
53 | 55 | ControllerException::NOT_FOUND |
54 | 56 | ); |
55 | 57 | } |
56 | 58 | |
57 | - if (is_object($value)) { |
|
59 | + if (is_object($value)) |
|
60 | + { |
|
58 | 61 | // pre-filled |
59 | 62 | continue; |
60 | 63 | } |
61 | 64 | |
62 | 65 | $entity = $this->resolveEntity($role, $value); |
63 | - if ($entity === null) { |
|
66 | + if ($entity === null) |
|
67 | + { |
|
64 | 68 | throw new ControllerException( |
65 | 69 | "Entity `{$parameter}` can not be found", |
66 | 70 | ControllerException::NOT_FOUND |
@@ -81,7 +85,8 @@ discard block |
||
81 | 85 | */ |
82 | 86 | protected function getParameter(string $role, array $parameters, bool $useDefault = false) |
83 | 87 | { |
84 | - if (!$useDefault) { |
|
88 | + if (!$useDefault) |
|
89 | + { |
|
85 | 90 | return $parameters[$role] ?? null; |
86 | 91 | } |
87 | 92 | |
@@ -106,23 +111,30 @@ discard block |
||
106 | 111 | private function getDeclaredEntities(string $controller, string $action): array |
107 | 112 | { |
108 | 113 | $key = sprintf('%s:%s', $controller, $action); |
109 | - if (array_key_exists($key, $this->cache)) { |
|
114 | + if (array_key_exists($key, $this->cache)) |
|
115 | + { |
|
110 | 116 | return $this->cache[$key]; |
111 | 117 | } |
112 | 118 | |
113 | 119 | $this->cache[$key] = []; |
114 | - try { |
|
120 | + try |
|
121 | + { |
|
115 | 122 | $method = new \ReflectionMethod($controller, $action); |
116 | - } catch (\ReflectionException $e) { |
|
123 | + } |
|
124 | + catch (\ReflectionException $e) |
|
125 | + { |
|
117 | 126 | return []; |
118 | 127 | } |
119 | 128 | |
120 | - foreach ($method->getParameters() as $parameter) { |
|
121 | - if ($parameter->getClass() === null) { |
|
129 | + foreach ($method->getParameters() as $parameter) |
|
130 | + { |
|
131 | + if ($parameter->getClass() === null) |
|
132 | + { |
|
122 | 133 | continue; |
123 | 134 | } |
124 | 135 | |
125 | - if ($this->orm->getSchema()->defines($parameter->getClass()->getName())) { |
|
136 | + if ($this->orm->getSchema()->defines($parameter->getClass()->getName())) |
|
137 | + { |
|
126 | 138 | $this->cache[$key][$parameter->getName()] = $this->orm->resolveRole($parameter->getClass()->getName()); |
127 | 139 | } |
128 | 140 | } |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | { |
53 | 53 | $permission = $this->getPermissions($controller, $action); |
54 | 54 | |
55 | - if ($permission !== null && !$this->guard->allows($permission[0], $parameters)) { |
|
55 | + if ($permission !== null && !$this->guard->allows($permission[0], $parameters)){ |
|
56 | 56 | throw new ControllerException( |
57 | 57 | sprintf( |
58 | 58 | 'Unauthorized permission `%s` for action `%s`->`%s`', |
@@ -79,14 +79,14 @@ discard block |
||
79 | 79 | private function getPermissions(string $controller, string $action): ?array |
80 | 80 | { |
81 | 81 | $key = sprintf('%s:%s', $controller, $action); |
82 | - if (array_key_exists($key, $this->cache)) { |
|
82 | + if (array_key_exists($key, $this->cache)){ |
|
83 | 83 | return $this->cache[$key]; |
84 | 84 | } |
85 | 85 | |
86 | 86 | $this->cache[$key] = null; |
87 | - try { |
|
87 | + try{ |
|
88 | 88 | $method = new \ReflectionMethod($controller, $action); |
89 | - } catch (\ReflectionException $e) { |
|
89 | + }catch (\ReflectionException $e){ |
|
90 | 90 | return null; |
91 | 91 | } |
92 | 92 | |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | Guarded::class |
105 | 105 | ); |
106 | 106 | |
107 | - if ($action === null) { |
|
107 | + if ($action === null){ |
|
108 | 108 | return null; |
109 | 109 | } |
110 | 110 | |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | ControllerException::FORBIDDEN |
127 | 127 | ]; |
128 | 128 | |
129 | - if ($guarded->permission === null && $ns === null) { |
|
129 | + if ($guarded->permission === null && $ns === null){ |
|
130 | 130 | throw new InterceptorException(sprintf( |
131 | 131 | 'Unable to apply @Guarded without name or @GuardNamespace on `%s`->`%s`', |
132 | 132 | $method->getDeclaringClass()->getName(), |
@@ -134,16 +134,16 @@ discard block |
||
134 | 134 | )); |
135 | 135 | } |
136 | 136 | |
137 | - if ($ns !== null) { |
|
137 | + if ($ns !== null){ |
|
138 | 138 | $permission[0] = sprintf('%s.%s', $ns->namespace, $permission[0]); |
139 | 139 | } |
140 | 140 | |
141 | - if ($this->namespace !== null) { |
|
141 | + if ($this->namespace !== null){ |
|
142 | 142 | // global namespace |
143 | 143 | $permission[0] = sprintf('%s.%s', $this->namespace, $permission[0]); |
144 | 144 | } |
145 | 145 | |
146 | - switch ($guarded->else) { |
|
146 | + switch ($guarded->else){ |
|
147 | 147 | case 'badAction': |
148 | 148 | $permission[1] = ControllerException::BAD_ACTION; |
149 | 149 | break; |
@@ -52,7 +52,8 @@ discard block |
||
52 | 52 | { |
53 | 53 | $permission = $this->getPermissions($controller, $action); |
54 | 54 | |
55 | - if ($permission !== null && !$this->guard->allows($permission[0], $parameters)) { |
|
55 | + if ($permission !== null && !$this->guard->allows($permission[0], $parameters)) |
|
56 | + { |
|
56 | 57 | throw new ControllerException( |
57 | 58 | sprintf( |
58 | 59 | 'Unauthorized permission `%s` for action `%s`->`%s`', |
@@ -79,14 +80,18 @@ discard block |
||
79 | 80 | private function getPermissions(string $controller, string $action): ?array |
80 | 81 | { |
81 | 82 | $key = sprintf('%s:%s', $controller, $action); |
82 | - if (array_key_exists($key, $this->cache)) { |
|
83 | + if (array_key_exists($key, $this->cache)) |
|
84 | + { |
|
83 | 85 | return $this->cache[$key]; |
84 | 86 | } |
85 | 87 | |
86 | 88 | $this->cache[$key] = null; |
87 | - try { |
|
89 | + try |
|
90 | + { |
|
88 | 91 | $method = new \ReflectionMethod($controller, $action); |
89 | - } catch (\ReflectionException $e) { |
|
92 | + } |
|
93 | + catch (\ReflectionException $e) |
|
94 | + { |
|
90 | 95 | return null; |
91 | 96 | } |
92 | 97 | |
@@ -104,7 +109,8 @@ discard block |
||
104 | 109 | Guarded::class |
105 | 110 | ); |
106 | 111 | |
107 | - if ($action === null) { |
|
112 | + if ($action === null) |
|
113 | + { |
|
108 | 114 | return null; |
109 | 115 | } |
110 | 116 | |
@@ -126,7 +132,8 @@ discard block |
||
126 | 132 | ControllerException::FORBIDDEN |
127 | 133 | ]; |
128 | 134 | |
129 | - if ($guarded->permission === null && $ns === null) { |
|
135 | + if ($guarded->permission === null && $ns === null) |
|
136 | + { |
|
130 | 137 | throw new InterceptorException(sprintf( |
131 | 138 | 'Unable to apply @Guarded without name or @GuardNamespace on `%s`->`%s`', |
132 | 139 | $method->getDeclaringClass()->getName(), |
@@ -134,16 +141,19 @@ discard block |
||
134 | 141 | )); |
135 | 142 | } |
136 | 143 | |
137 | - if ($ns !== null) { |
|
144 | + if ($ns !== null) |
|
145 | + { |
|
138 | 146 | $permission[0] = sprintf('%s.%s', $ns->namespace, $permission[0]); |
139 | 147 | } |
140 | 148 | |
141 | - if ($this->namespace !== null) { |
|
149 | + if ($this->namespace !== null) |
|
150 | + { |
|
142 | 151 | // global namespace |
143 | 152 | $permission[0] = sprintf('%s.%s', $this->namespace, $permission[0]); |
144 | 153 | } |
145 | 154 | |
146 | - switch ($guarded->else) { |
|
155 | + switch ($guarded->else) |
|
156 | + { |
|
147 | 157 | case 'badAction': |
148 | 158 | $permission[1] = ControllerException::BAD_ACTION; |
149 | 159 | break; |
@@ -46,9 +46,9 @@ |
||
46 | 46 | DirectoriesInterface $dirs, |
47 | 47 | FilesInterface $files, |
48 | 48 | LoggerInterface $logger = null |
49 | - ) { |
|
49 | + ){ |
|
50 | 50 | return new FileSnapshotter( |
51 | - $dirs->get('runtime') . '/snapshots/', |
|
51 | + $dirs->get('runtime').'/snapshots/', |
|
52 | 52 | $env->get('SNAPSHOT_MAX_FILES', self::MAX_SNAPSHOTS), |
53 | 53 | $env->get('SNAPSHOT_VERBOSITY', HandlerInterface::VERBOSITY_VERBOSE), |
54 | 54 | new HtmlHandler(), |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | HandlerInterface $handler, |
54 | 54 | FilesInterface $files, |
55 | 55 | LoggerInterface $logger = null |
56 | - ) { |
|
56 | + ){ |
|
57 | 57 | $this->directory = $directory; |
58 | 58 | $this->maxFiles = $maxFiles; |
59 | 59 | $this->verbosity = $verbosity; |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | { |
70 | 70 | $snapshot = new Snapshot($this->getID($e), $e); |
71 | 71 | |
72 | - if ($this->logger !== null) { |
|
72 | + if ($this->logger !== null){ |
|
73 | 73 | $this->logger->error($snapshot->getMessage()); |
74 | 74 | } |
75 | 75 | |
@@ -101,17 +101,17 @@ discard block |
||
101 | 101 | protected function rotateSnapshots(): void |
102 | 102 | { |
103 | 103 | $finder = new Finder(); |
104 | - $finder->in($this->directory)->sort(function (SplFileInfo $a, SplFileInfo $b) { |
|
104 | + $finder->in($this->directory)->sort(function (SplFileInfo $a, SplFileInfo $b){ |
|
105 | 105 | return $b->getMTime() - $a->getMTime(); |
106 | 106 | }); |
107 | 107 | |
108 | 108 | $count = 0; |
109 | - foreach ($finder as $file) { |
|
109 | + foreach ($finder as $file){ |
|
110 | 110 | $count++; |
111 | - if ($count > $this->maxFiles) { |
|
112 | - try { |
|
111 | + if ($count > $this->maxFiles){ |
|
112 | + try{ |
|
113 | 113 | $this->files->delete($file->getRealPath()); |
114 | - } catch (FilesException $e) { |
|
114 | + }catch (FilesException $e){ |
|
115 | 115 | // ignore |
116 | 116 | } |
117 | 117 | } |
@@ -69,7 +69,8 @@ discard block |
||
69 | 69 | { |
70 | 70 | $snapshot = new Snapshot($this->getID($e), $e); |
71 | 71 | |
72 | - if ($this->logger !== null) { |
|
72 | + if ($this->logger !== null) |
|
73 | + { |
|
73 | 74 | $this->logger->error($snapshot->getMessage()); |
74 | 75 | } |
75 | 76 | |
@@ -101,17 +102,23 @@ discard block |
||
101 | 102 | protected function rotateSnapshots(): void |
102 | 103 | { |
103 | 104 | $finder = new Finder(); |
104 | - $finder->in($this->directory)->sort(function (SplFileInfo $a, SplFileInfo $b) { |
|
105 | + $finder->in($this->directory)->sort(function (SplFileInfo $a, SplFileInfo $b) |
|
106 | + { |
|
105 | 107 | return $b->getMTime() - $a->getMTime(); |
106 | 108 | }); |
107 | 109 | |
108 | 110 | $count = 0; |
109 | - foreach ($finder as $file) { |
|
111 | + foreach ($finder as $file) |
|
112 | + { |
|
110 | 113 | $count++; |
111 | - if ($count > $this->maxFiles) { |
|
112 | - try { |
|
114 | + if ($count > $this->maxFiles) |
|
115 | + { |
|
116 | + try |
|
117 | + { |
|
113 | 118 | $this->files->delete($file->getRealPath()); |
114 | - } catch (FilesException $e) { |
|
119 | + } |
|
120 | + catch (FilesException $e) |
|
121 | + { |
|
115 | 122 | // ignore |
116 | 123 | } |
117 | 124 | } |