Architectural struggle
The attachment named example.php is a simple php script. It's job is to serve one json-
encoded string in response to http request. The script is also using examples.csv for fetching
data. Your job is to refactor this script. Remove all mistakes and bad practices, create some
structure of folders and files that will contain classes, design architecture as if this simple script
was a small part of a much bigger project. In other words: pimp this script up! This is more about
architecture of your code and less about function itself. We will judge on structure, hierarchy of
objects and overall design.
Take into consideration how clean your code is and how easy it is
to read and understand. Avoid tight coupling between any layer you introduce to this code and
remember that this code should be easy for further extension by somebody else. You have total
freedom in how to achieve this.
There are, however, some rules as described below:
You may not use any external library or framework except from the ones used to Unit Test the solution.
After refactoring, this code must do the same thing.
You have to use MVC pattern (or similar).
You could use any other design pattern for the rest of the architecture if you feel like it.
You should conform to programming principles.
You have to use OOP.
You have to do it in RESTful style.
You should introduce some framework-like feature (routing, dispatching, autoloading).
You have to use at least php 5.3 (namespace is mandatory).
You can use psr-0 autoloading system.
You can put comments where you describe why you do certain things or just explaining some more high level decision.
You can change storage system for contact data.
You could implement different presentation formats, allowing for different content-types to be served.
All above rules are very important!
Have to, may not- this is mandatory
could, should - this is strongly recommended
can - it's up to you
Optional Desert - The icing on the cake
Using the code created in the previous part, add full CRUD feature to it. You may change the data
storage system if you want. In the end the system should allow to add a new entry and/or
remove update existing ones. There is no need to create any form of UI.
Final notes:
Remember that this is web based application, http statuses are important, sometimes it’s the only mean of communication with your consumer.
Code should have some basic exception control and data validation.
You can use REST architecture (it will be a plus
Design of this code is more important that it’s function. Focus on good, clean architecture!
Follow programming principles! If you don’t know what that is search SOLID in object oriented design.
Support
If you have any questions, please contact me via email.
Ejemplo a refactorizar
<?php
$path = $_SERVER['PATH_INFO'];
if ($path = '/address')
{
$controller = new \Controller();
$return = $controller->ex();
echo $return;
}
class Controller
{
var $addresses = [];
function ex()
{
$this->rcd();
$id = $_GET['id'];
$address = $this->addresses[$id];
return json_encode($address);
}
function rcd()
{
$file = fopen('example.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
$this->addresses[] = [
"name" => $line[0],
"phone" => $line[1],
"street" => $line[2]
];
}
fclose($file);
}
}
?>
Datos:
Michal,506088156,Michalowskiego 41
Marcin,502145785,Opata Rybickiego 1
Piotr,504212369,Horacego 23
Albert,605458547,Jan Pawla 67