GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 04010b...632dca )
by Cees-Jan
05:30
created

EtcPasswdMiddleware::createShadowContents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\Psr15\EtcPasswd;
4
5
use Interop\Http\Server\MiddlewareInterface;
6
use Interop\Http\Server\RequestHandlerInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Zend\Diactoros\Response;
10
use Zend\Diactoros\Stream;
11
12
final class EtcPasswdMiddleware implements MiddlewareInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private $passwdContents = '';
18
19
    /**
20
     * @var string
21
     */
22
    private $shadowContents = '';
23
24
    public function __construct(iterable $users)
25
    {
26
        $this->passwdContents = implode(
27
            PHP_EOL,
28
            iterator_to_array($this->createPasswdContents($users))
29
        );
30
        $this->shadowContents = implode(
31
            PHP_EOL,
32
            iterator_to_array($this->createShadowContents($users))
33
        );
34
    }
35
36
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
37
    {
38 View Code Duplication
        if ($request->getMethod() === 'GET' && $request->getUri()->getPath() === '/etc/passwd') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
            return $this->createResponse($this->passwdContents);
40
        }
41
42 View Code Duplication
        if ($request->getMethod() === 'GET' && $request->getUri()->getPath() === '/etc/shadow') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
            return $this->createResponse($this->shadowContents);
44
        }
45
46
        return $handler->handle($request);
47
    }
48
49
    private function createPasswdContents(iterable $users): iterable
50
    {
51
        foreach ($users as $user => $password) {
52
            yield $user . ':x:' . crc32($user) . ':0:99999:7:::';
53
        }
54
    }
55
56
    private function createShadowContents(iterable $users): iterable
57
    {
58
        foreach ($users as $user => $password) {
59
            yield $user . ':$1$$' . base64_encode(md5($password)) . ':' . crc32($user) . ':0:99999:7:::';
60
        }
61
    }
62
63
    private function createResponse(string $contents): ResponseInterface
64
    {
65
        $body = new Stream('php://temp', 'wb+');
66
        $body->write($contents);
67
        $body->rewind();
68
69
        return (new Response())->
70
            withStatus(200)->
71
            withHeader('Content-Type', 'text/plain')->
72
            withBody($body);
73
    }
74
}
75