Skip to content

Commit 297c9aa

Browse files
committed
move repetitive code to trait when possible
1 parent 7d94674 commit 297c9aa

File tree

4 files changed

+86
-109
lines changed

4 files changed

+86
-109
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ return [
7676
'value' => 'max-age=31536000',
7777
],
7878
],
79+
// ...
7980
];
8081
```
8182

src/HttpsTrait.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace ForceHttpsModule;
4+
5+
use Zend\Router\RouteMatch;
6+
use Zend\Expressive\Router\RouteResult;
7+
8+
trait HttpsTrait
9+
{
10+
/**
11+
* Is Scheme https ?
12+
*
13+
* @param string $uriScheme
14+
*
15+
* @return bool
16+
*/
17+
private function isSchemeHttps($uriScheme)
18+
{
19+
return $uriScheme === 'https';
20+
}
21+
22+
/**
23+
* Check Config if is going to be forced to https.
24+
*
25+
* @param RouteMatch|RouteResult $match
26+
*
27+
* @return bool
28+
*/
29+
private function isGoingToBeForcedToHttps($match)
30+
{
31+
if (! $this->config['force_all_routes'] &&
32+
! in_array(
33+
$match->getMatchedRouteName(),
34+
$this->config['force_specific_routes']
35+
)
36+
) {
37+
return false;
38+
}
39+
40+
return true;
41+
}
42+
43+
/**
44+
* Validate Scheme and Forced Https Config
45+
*
46+
* @param string $uriScheme
47+
* @param RouteMatch|RouteResult $match
48+
*
49+
* @return bool
50+
*/
51+
private function validateSchemeAndToBeForcedHttpsConfig($uriScheme, $match)
52+
{
53+
if ($this->isSchemeHttps($uriScheme) || ! $this->isGoingToBeForcedToHttps($match)) {
54+
return false;
55+
}
56+
57+
return true;
58+
}
59+
}

src/Listener/ForceHttps.php

Lines changed: 12 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22

33
namespace ForceHttpsModule\Listener;
44

5+
use ForceHttpsModule\HttpsTrait;
56
use Zend\Console\Console;
67
use Zend\EventManager\AbstractListenerAggregate;
78
use Zend\EventManager\EventManagerInterface;
89
use Zend\Http\PhpEnvironment\Response;
910
use Zend\Mvc\MvcEvent;
11+
use Zend\Router\RouteMatch;
1012

1113
class ForceHttps extends AbstractListenerAggregate
1214
{
15+
use HttpsTrait;
16+
1317
/**
1418
* @var array
1519
*/
1620
private $config;
1721

1822
/**
19-
* @method __construct
2023
* @param array $config
2124
*/
2225
public function __construct(array $config)
@@ -37,50 +40,18 @@ public function attach(EventManagerInterface $events, $priority = 1)
3740
$this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, [$this, 'forceHttpsScheme']);
3841
}
3942

40-
/**
41-
* Is Scheme https ?
42-
*
43-
* @param string $uriScheme
44-
*
45-
* @return bool
46-
*/
47-
private function isSchemeHttps($uriScheme)
48-
{
49-
return $uriScheme === 'https';
50-
}
51-
52-
/**
53-
* Check Config if is going to be forced to https.
54-
*
55-
* @param MvcEvent $e
56-
* @return bool
57-
*/
58-
private function isGoingToBeForcedToHttps(MvcEvent $e)
59-
{
60-
if (! $this->config['force_all_routes'] &&
61-
! in_array(
62-
$e->getRouteMatch()->getMatchedRouteName(),
63-
$this->config['force_specific_routes']
64-
)
65-
) {
66-
return false;
67-
}
68-
69-
return true;
70-
}
71-
7243
/**
7344
* Set The HTTP Strict Transport Security.
7445
*
75-
* @param string $uriScheme
76-
* @param MvcEvent $e
77-
* @param Response $response
46+
* @param string $uriScheme
47+
* @param RouteMatch $match didn't typed hinted in code in favor of zf-mvc ^2.5 RouteMatch compat
48+
* @param Response $response
7849
*/
79-
private function setHttpStrictTransportSecurity($uriScheme, MvcEvent $e, Response $response)
50+
private function setHttpStrictTransportSecurity($uriScheme, $match, Response $response)
8051
{
8152
if (
8253
$this->isSchemeHttps($uriScheme) &&
83-
$this->isGoingToBeForcedToHttps($e) &&
54+
$this->isGoingToBeForcedToHttps($match) &&
8455
isset(
8556
$this->config['strict_transport_security']['enable'],
8657
$this->config['strict_transport_security']['value']
@@ -99,23 +70,6 @@ private function setHttpStrictTransportSecurity($uriScheme, MvcEvent $e, Respons
9970
}
10071
}
10172

102-
/**
103-
* Validate Scheme and Forced Https Config
104-
*
105-
* @param string $uriScheme
106-
* @param MvcEvent $e
107-
*
108-
* @return bool
109-
*/
110-
private function validateSchemeAndToBeForcedHttpsConfig($uriScheme, $e)
111-
{
112-
if ($this->isSchemeHttps($uriScheme) || ! $this->isGoingToBeForcedToHttps($e)) {
113-
return false;
114-
}
115-
116-
return true;
117-
}
118-
11973
/**
12074
* Force Https Scheme handle.
12175
*
@@ -135,8 +89,9 @@ public function forceHttpsScheme(MvcEvent $e)
13589
$uri = $request->getUri();
13690
$uriScheme = $uri->getScheme();
13791

138-
$this->setHttpStrictTransportSecurity($uriScheme, $e, $response);
139-
if (! $this->validateSchemeAndToBeForcedHttpsConfig($uriScheme, $e)) {
92+
$routeMatch = $e->getRouteMatch();
93+
$this->setHttpStrictTransportSecurity($uriScheme, $routeMatch, $response);
94+
if (! $this->validateSchemeAndToBeForcedHttpsConfig($uriScheme, $routeMatch)) {
14095
return;
14196
}
14297

src/Middleware/ForceHttps.php

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,40 @@
22

33
namespace ForceHttpsModule\Middleware;
44

5+
use ForceHttpsModule\HttpsTrait;
56
use Zend\Console\Console;
67
use Zend\Expressive\Router\RouterInterface;
8+
use Zend\Expressive\Router\RouteResult;
79

810
class ForceHttps
911
{
12+
use HttpsTrait;
13+
14+
/** @var array */
1015
private $config;
1116

17+
/** @var RouterInterface */
1218
private $router;
1319

20+
/**
21+
* @param array $config
22+
* @param RouterInterface $router
23+
*/
1424
public function __construct(array $config, RouterInterface $router)
1525
{
1626
$this->config = $config;
1727
$this->router = $router;
1828
}
1929

20-
/**
21-
* Is Scheme https ?
22-
*
23-
* @param string $uriScheme
24-
*
25-
* @return bool
26-
*/
27-
private function isSchemeHttps($uriScheme)
28-
{
29-
return $uriScheme === 'https';
30-
}
31-
32-
/**
33-
* Check Config if is going to be forced to https.
34-
*
35-
* @param $match
36-
* @return bool
37-
*/
38-
private function isGoingToBeForcedToHttps($match)
39-
{
40-
if (! $this->config['force_all_routes'] &&
41-
! in_array(
42-
$match->getMatchedRouteName(),
43-
$this->config['force_specific_routes']
44-
)
45-
) {
46-
return false;
47-
}
48-
49-
return true;
50-
}
51-
5230
/**
5331
* Set The HTTP Strict Transport Security.
5432
*
55-
* @param string $uriScheme
56-
* @param $match
33+
* @param string $uriScheme
34+
* @param RouteResult $match
35+
*
5736
* @param $response
5837
*/
59-
private function setHttpStrictTransportSecurity($uriScheme, $match, $response)
38+
private function setHttpStrictTransportSecurity($uriScheme, RouteResult $match, $response)
6039
{
6140
if (
6241
$this->isSchemeHttps($uriScheme) &&
@@ -78,23 +57,6 @@ private function setHttpStrictTransportSecurity($uriScheme, $match, $response)
7857
return $response;
7958
}
8059

81-
/**
82-
* Validate Scheme and Forced Https Config
83-
*
84-
* @param string $uriScheme
85-
* @param $match
86-
*
87-
* @return bool
88-
*/
89-
private function validateSchemeAndToBeForcedHttpsConfig($uriScheme, $match)
90-
{
91-
if ($this->isSchemeHttps($uriScheme) || ! $this->isGoingToBeForcedToHttps($match)) {
92-
return false;
93-
}
94-
95-
return true;
96-
}
97-
9860
public function __invoke($request, $response, callable $next = null)
9961
{
10062
$match = $this->router->match($request);

0 commit comments

Comments
 (0)