Completed
Push — master ( 48a5d3...028360 )
by Aydin
03:14
created

TimeServer::configure()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 56
Code Lines 29

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 56
rs 9.7251
cc 3
eloc 29
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpSchool\LearnYouPhp\Exercise;
4
5
use Hoa\Core\Exception\Exception;
6
use PhpSchool\LearnYouPhp\TcpSocketFactory;
7
use PhpSchool\PhpWorkshop\Event\CliExecuteEvent;
8
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
9
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
10
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
11
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
12
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
13
use PhpSchool\PhpWorkshop\Result\Failure;
14
use PhpSchool\PhpWorkshop\Result\StdOutFailure;
15
use PhpSchool\PhpWorkshop\Result\Success;
16
use Psr\Http\Message\RequestInterface;
17
use Symfony\Component\Process\Process;
18
use Symfony\Component\Process\ProcessBuilder;
19
use Zend\Diactoros\Request;
20
21
/**
22
 * Class TimeServer
23
 * @package PhpSchool\LearnYouPhp\Exercise
24
 * @author Aydin Hassan <[email protected]>
25
 */
26
class TimeServer extends AbstractExercise implements ExerciseInterface, CliExercise
27
{
28
29
    /**
30
     * @var TcpSocketFactory
31
     */
32
    private $socketFactory;
33
34
    /**
35
     * TimeServer constructor.
36
     * @param TcpSocketFactory $socketFactory
37
     */
38
    public function __construct(TcpSocketFactory $socketFactory)
39
    {
40
        $this->socketFactory = $socketFactory;
41
    }
42
    
43
    /**
44
     * @return string
45
     */
46
    public function getName()
47
    {
48
        return 'Time Server';
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getDescription()
55
    {
56
        return 'Build a Time Server!';
57
    }
58
59
    /**
60
     * @param ExerciseDispatcher $exerciseDispatcher
61
     */
62
    public function configure(ExerciseDispatcher $exerciseDispatcher)
63
    {
64
        $eventDispatcher = $exerciseDispatcher->getEventDispatcher();
65
66
        $eventDispatcher->listen('cli.verify.solution-execute.pre', function (CliExecuteEvent $event) {
67
            $event->appendArg('127.0.0.1');
68
            $event->appendArg($this->getRandomPort());
69
        });
70
71
        $eventDispatcher->listen('cli.verify.user-execute.pre', function (CliExecuteEvent $event) {
72
            $event->appendArg('127.0.0.1');
73
            $event->appendArg($this->getRandomPort());
74
        });
75
76
        $eventDispatcher->listen('cli.verify.solution.executing', function (CliExecuteEvent $event) {
77
            $args   = $event->getArgs()->getArrayCopy();
78
            $client = $this->socketFactory->createClient(...$args);
0 ignored issues
show
Bug introduced by
The call to createClient() misses a required argument $port.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$args is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
80
            //wait for server to boot
81
            usleep(100000);
82
83
            $client->connect();
84
            $client->readAll();
85
86
            //wait for shutdown
87
            usleep(100000);
88
        });
89
90
        $eventDispatcher->insertVerifier('cli.verify.user.executing', function (CliExecuteEvent $event) {
91
            $args   = $event->getArgs()->getArrayCopy();
92
            $client = $this->socketFactory->createClient(...$args);
0 ignored issues
show
Bug introduced by
The call to createClient() misses a required argument $port.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$args is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
94
            //wait for server to boot
95
            usleep(100000);
96
97
            try {
98
                $client->connect();
99
            } catch (Exception $e) {
100
                return Failure::fromNameAndReason($this->getName(), $e->getMessage());
101
            }
102
103
            $out = $client->readAll();
104
105
            //wait for shutdown
106
            usleep(100000);
107
108
            $date = new \DateTime;
109
110
            //match the current date but any seconds
111
            //since we can't mock time in PHP easily
112
            if (!preg_match(sprintf('/^%s:([0-5][0-9]|60)\n$/', $date->format('Y-m-d H:i')), $out)) {
113
                return new StdOutFailure($this->getName(), $date->format("Y-m-d H:i:s\n"), $out);
114
            }
115
            return new Success($this->getName());
116
        });
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    private function getRandomPort()
123
    {
124
        return (string) mt_rand(1025, 65535);
125
    }
126
127
    /**
128
     * @return ExerciseType
129
     */
130
    public function getType()
131
    {
132
        return ExerciseType::CLI();
133
    }
134
135
    /**
136
     * @return string[]
137
     */
138
    public function getArgs()
139
    {
140
        return [];
141
    }
142
}
143