This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lucaminudel#16 from arthuralmeidap/php-version
Implemented PHP exercises version
- Loading branch information
Showing
8 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace TDDMicroExercises\PHP\TelemetrySystem; | ||
|
||
class TelemetryClient | ||
{ | ||
const DiagnosticMessage = "AT#UD"; | ||
|
||
private $_onlineStatus; | ||
private $_diagnosticMessageJustSent = false; | ||
|
||
/*private $_connectionEventsSimulator = new Random(); | ||
private $_randomMessageSimulator = new Random();*/ | ||
|
||
public function getOnlineStatus() | ||
{ | ||
return $this->_onlineStatus; | ||
} | ||
|
||
public function connect( $telemetryServerConnectionString ) | ||
{ | ||
if( is_null($telemetryServerConnectionString) || empty($telemetryServerConnectionString) ) | ||
{ | ||
throw new \InvalidArgumentException(); | ||
} | ||
|
||
// Fake the connection with 20% chances of success | ||
//$success = rand(1, 10) <= 2; | ||
$success = rand(1, 10) <= 2; | ||
$this->_onlineStatus = $success; | ||
} | ||
|
||
public function disconnect() | ||
{ | ||
$this->_onlineStatus = false; | ||
} | ||
|
||
public function send( $message ) | ||
{ | ||
if( is_null($message) || empty($message) ) { | ||
throw new \InvalidArgumentException(); | ||
} | ||
|
||
// The simulation of Send() actually just remember if the last message sent was a diagnostic message. | ||
// This information will be used to simulate the Receive(). Indeed there is no real server listening. | ||
if ( $message == TelemetryClient::DiagnosticMessage ) | ||
{ | ||
$this->_diagnosticMessageJustSent = true; | ||
|
||
} | ||
else | ||
{ | ||
$this->_diagnosticMessageJustSent = false; | ||
} | ||
|
||
} | ||
|
||
public function receive() | ||
{ | ||
$message = ''; | ||
|
||
if($this->_diagnosticMessageJustSent) | ||
{ | ||
// Simulate the reception of the diagnostic message | ||
$message = "LAST TX rate................ 100 MBPS\r\n" | ||
. "HIGHEST TX rate............. 100 MBPS\r\n" | ||
. "LAST RX rate................ 100 MBPS\r\n" | ||
. "HIGHEST RX rate............. 100 MBPS\r\n" | ||
. "BIT RATE.................... 100000000\r\n" | ||
. "WORD LEN.................... 16\r\n" | ||
. "WORD/FRAME.................. 511\r\n" | ||
. "BITS/FRAME.................. 8192\r\n" | ||
. "MODULATION TYPE............. PCM/FM\r\n" | ||
. "TX Digital Los.............. 0.75\r\n" | ||
. "RX Digital Los.............. 0.10\r\n" | ||
. "BEP Test.................... -5\r\n" | ||
. "Local Rtrn Count............ 00\r\n" | ||
. "Remote Rtrn Count........... 00"; | ||
|
||
$this->_diagnosticMessageJustSent = false; | ||
} | ||
else | ||
{ | ||
$messageLength = rand( 50, 110 ); | ||
for($i = $messageLength; $i > 0; $i-- ) | ||
{ | ||
$message += chr( rand( 40, 126 ) ) ; | ||
} | ||
} | ||
|
||
return $message; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
TDDMicroExercises/PHP/TelemetrySystem/TelemetryDiagnosticControls.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace TDDMicroExercises\PHP\TelemetrySystem; | ||
|
||
class TelemetryDiagnosticControls | ||
{ | ||
const DiagnosticChannelConnectionString = "*111#"; | ||
|
||
private $_telemetryClient; | ||
private $_diagnosticInfo = ''; | ||
|
||
public function __construct() | ||
{ | ||
$this->_telemetryClient = new TelemetryClient(); | ||
} | ||
|
||
public function getDiagnosticInfo() | ||
{ | ||
return $this->_diagnosticInfo; | ||
} | ||
|
||
public function setDiagnosticInfo( $diagnosticInfo ) | ||
{ | ||
$this->_diagnosticInfo = $diagnosticInfo; | ||
} | ||
|
||
public function checkTransmission() | ||
{ | ||
$this->_diagnosticInfo = ''; | ||
$this->_telemetryClient->disconnect(); | ||
|
||
$retryLeft = 3; | ||
while ( $this->_telemetryClient->getOnlineStatus() == false && $retryLeft > 0 ) | ||
{ | ||
$this->_telemetryClient->connect( | ||
TelemetryDiagnosticControls::DiagnosticChannelConnectionString | ||
); | ||
$retryLeft -= 1; | ||
} | ||
|
||
if($this->_telemetryClient->getOnlineStatus() == false) | ||
{ | ||
throw new \Exception('Unable to connect.'); | ||
} | ||
|
||
$this->_telemetryClient->send(TelemetryClient::DiagnosticMessage); | ||
$this->_diagnosticInfo = $this->_telemetryClient->receive(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
TDDMicroExercises/PHP/TirePressureMonitoringSystem/Alarm.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace TDDMicroExercises\PHP\TirePressureMonitoringSystem; | ||
|
||
class Alarm | ||
{ | ||
const LOW_PRESSURE_TRESHOLD = 17; | ||
const HIGH_PRESSURE_TRESHOLD = 21; | ||
|
||
private $sensor; | ||
private $alarmOn; | ||
private $alarmCount; | ||
|
||
public function __construct() { | ||
$this->sensor = new Sensor(); | ||
$this->alarmOn = false; | ||
$this->alarmCount = 0; | ||
} | ||
|
||
|
||
public function check() | ||
{ | ||
$psiPressureValue = $this->sensor->popNextPressurePsiValue(); | ||
|
||
if ($psiPressureValue < Alarm::LOW_PRESSURE_TRESHOLD | ||
|| Alarm::HIGH_PRESSURE_TRESHOLD < $psiPressureValue) { | ||
|
||
$this->alarmOn = true; | ||
$this->alarmCount += 1; | ||
} | ||
} | ||
|
||
public function alarmOn() | ||
{ | ||
return $this->alarmOn; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
TDDMicroExercises/PHP/TirePressureMonitoringSystem/Sensor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace TDDMicroExercises\PHP\TirePressureMonitoringSystem; | ||
|
||
class Sensor | ||
{ | ||
const OFFSET = 16; | ||
|
||
public static function SamplePressure() | ||
{ | ||
// placeholder implementation that simulate a real sensor in a real tire | ||
$pressureTelemetryValue = floor(6 * rand() * rand()); | ||
return $pressureTelemetryValue; | ||
} | ||
|
||
public function popNextPressurePsiValue() | ||
{ | ||
$pressureTelemetryValue = self::SamplePressure(); | ||
return self::OFFSET + $pressureTelemetryValue; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
TDDMicroExercises/PHP/TurnTicketDispenser/TicketDispenser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace TDDMicroExercises\PHP\TurnTicketDispenser; | ||
|
||
class TicketDispenser | ||
{ | ||
|
||
public function getTurnTicket() | ||
{ | ||
$newTurnNumber = TurnNumberSequence::getNextTurnNumber(); | ||
$turnTicket = new TurnTicket($newTurnNumber); | ||
|
||
return $turnTicket; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
TDDMicroExercises/PHP/TurnTicketDispenser/TurnNumberSequence.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace TDDMicroExercises\PHP\TurnTicketDispenser; | ||
|
||
class TurnNumberSequence | ||
{ | ||
private static $_turnNumber = 0; | ||
|
||
public static function getNextTurnNumber() | ||
{ | ||
return self::$_turnNumber++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace TDDMicroExercises\PHP\TurnTicketDispenser; | ||
|
||
class TurnTicket | ||
{ | ||
private $_turnNumber; | ||
|
||
public function __construct($turnNumber) | ||
{ | ||
$this->_turnNumber = $turnNumber; | ||
} | ||
|
||
public function getTurnNumber() | ||
{ | ||
return $this->_turnNumber; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
TDDMicroExercises/PHP/UnicodeFileToHtmlTextConverter/UnicodeFileToHtmlTextConverter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace TDDMicroExercises\PHP\UnicodeFileToHtmlTextConverter; | ||
|
||
class UnicodeFileToHtmlTextConverter | ||
{ | ||
private $_fullFilenameWithPath; | ||
|
||
public function __construct($fullFilenameWithPath) | ||
{ | ||
$this->_fullFilenameWithPath = $fullFilenameWithPath; | ||
} | ||
|
||
public function convertToHtml() | ||
{ | ||
$unicodeFileStrem = fopen($this->_fullFilenameWithPath, 'r+'); | ||
$html = ''; | ||
|
||
while ( $line = fgets($unicodeFileStrem)) | ||
{ | ||
$html .= htmlentities($line); | ||
$html .= "<br />"; | ||
} | ||
|
||
fclose($unicodeFileStrem); | ||
|
||
return $html; | ||
} | ||
} |