Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions config/database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$database = [
'database_host' => 'localhost',
'database_name' => 'framework',
'username' => 'student',
'password' => 'password',
'charset' => 'utf8'
];
24 changes: 18 additions & 6 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
'controller' => 'index',
'action' => 'index'
],
'/hello' => [
'controller' => 'helloWorld',
'action' => 'hello'
'/contracts' => [
'controller' => 'contracts',
'action' => 'showContracts'
],
'/world' => [
'controller' => 'helloWorld',
'action' => 'world'
'/addcontract' => [
'controller' => 'contracts',
'action' => 'showContractsForm'
],
'/addsell' => [
'controller' => 'table',
'action' => 'addSell'
],
'/table' => [
'controller' => 'table',
'action' => 'showTable'
],
'/show' => [
'controller' => 'index',
'action' => 'show'
]
];
20 changes: 20 additions & 0 deletions controllers/BasicController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

class BasicController
{
protected function render($templateName, $vars = [])
{
ob_start();
extract($vars);
include sprintf('templates/%s.php', $templateName);
$content = ob_get_contents();
ob_end_clean();
return $content;
}

public function __call($name, $arguments)
{
return new Response('Sorry but this action not found',
'404', 'Not found');
}
}
38 changes: 38 additions & 0 deletions controllers/ContractsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
require_once 'entities/Contract.php';
require_once 'controllers/BasicController.php';
require_once 'controllers/errors_handling/ContractsErrorHandler.php';

class ContractsController extends BasicController
{

protected $contractsRepository;
protected $agentsRepository;

public function __construct(ContractsRepository $contractsRepository, AgentsRepository $agentsRepository)
{
$this->contractsRepository = $contractsRepository;
$this->agentsRepository = $agentsRepository;
}


public function showContractsAction(Request $request)
{
return new Response($this->render('contracts', ['contractsRepository' => $this->contractsRepository]));
}

public function showContractsFormAction(Request $request)
{
if ($request->isPost()) {
$contract = new Contract($request);

$errors = ContractsErrorHandler::getErrors($contract, $this->contractsRepository);
if ($errors !== null)
return new Response($this->render('add-contract', ['errors' => $errors]));

$this->contractsRepository->addContract($contract);
return new Response($this->render('contracts', ['contractsRepository' => $this->contractsRepository]));
}
return new Response($this->render('add-contract'));
}
}
54 changes: 0 additions & 54 deletions controllers/HelloWorldController.php

This file was deleted.

32 changes: 6 additions & 26 deletions controllers/IndexController.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
<?php
require_once 'controllers/BasicController.php';

class IndexController
class IndexController extends BasicController
{
/**
* Action name
* @var string
*/
public $name = 'index';

public function indexAction()
{
return new Response(
'Blank index'
);
}

protected function render($templateName, $vars = [])
public function indexAction(Request $request)
{
ob_start();
extract($vars);
include sprintf('templates/%s.php', $templateName);
$content = ob_get_contents();
ob_end_clean();
return $content;
return new Response($this->render('welcome'));
}

public function __call($name, $arguments)
public function errorAction(Request $request)
{
return new Response('Sorry but this action not found',
'404', 'Not found');
return new Response($this->render('404'));
}


}
50 changes: 50 additions & 0 deletions controllers/TableController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
require_once 'entities/Sell.php';
require_once 'controllers/BasicController.php';
require_once 'controllers/errors_handling/TableErrorHandler.php';

class TableController extends BasicController
{
/**
* Action name
* @var string
*/
public $name = 'index';

/**
* @var ArticleRepository
*/
protected $contractsRepository;
protected $sellsRepository;
protected $agentsRepository;

public function __construct(ContractsRepository $contractsRepository, SellsRepository $sellsRepository, AgentsRepository $agentsRepository)
{
$this->contractsRepository = $contractsRepository;
$this->sellsRepository = $sellsRepository;
$this->agentsRepository = $agentsRepository;
}


public function showTableAction(Request $request)
{
return new Response($this->render('comission-total', ['sellsRepository' => $this->sellsRepository, 'agentsRepository' => $this->agentsRepository]));
}

public function addSellAction(Request $request)
{
if ($request->isPost()) {

$sell = new Sell($request, $this->agentsRepository, $this->contractsRepository);

$errors = TableErrorHandler::getErrors($sell, $this->sellsRepository);
if ($errors !== null)
return new Response($this->render('add-sell', ['contractsRepository' => $this->contractsRepository, 'errors' => $errors]));


$this->sellsRepository->addNewSell($sell);
return new Response($this->render('comission-total', ['sellsRepository' => $this->sellsRepository, 'agentsRepository' => $this->agentsRepository]));
}
return new Response($this->render('add-sell', ['contractsRepository' => $this->contractsRepository]));
}
}
92 changes: 92 additions & 0 deletions controllers/errors_handling/ContractsErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

class ContractsErrorHandler
{
public static function getErrors(Contract $contract, ContractsRepository $contractsRepository)
{
$result = [];

foreach ((array)$contract as $key => $value) {
if ($value == '') {
array_push($result, ucfirst(str_replace('_', ' ', $key)) . ' can not be empty');
} else {
$funcName = 'check_' . $key;
$result = (new ContractsErrorHandler)->$funcName($contract, $contractsRepository, $result);
}
}

if (count($result) == 0)
return null;
return $result;
}

private function check_number($contract, ContractsRepository $contractsRepository, $result)
{
$contracts = $contractsRepository->getContractByNumber($contract->number);
if (count($contracts) !== 0)
array_push($result, 'Contract with number #' . $contract->number . ' is already exist');
return $result;
}

private function check_agent_name($contract, ContractsRepository $contractsRepository, $result)
{
$contractsOfAgentForThisComplex = $contractsRepository->getContractByAgentNameAndComplexName($contract->agent_name, $contract->living_complex);
if (count($contractsOfAgentForThisComplex) !== 0)
array_push($result, 'Agent already has contract for the complex ' . $contract->living_complex);
return $result;
}

private function check_living_complex($contract, ContractsRepository $contractsRepository, $result)
{
return $result;
}

private function check_award_type(Contract $contract, ContractsRepository $contractsRepository, $result)
{
if ($contract->award_type !== 'fix' && $contract->award_type !== 'percent')
array_push($result, 'Award type must be \'fix\' or \'percent\'');

return $result;
}

private function check_award_size(Contract $contract, ContractsRepository $contractsRepository, $result)
{
$message = '';
$is_award_correct = true;
if ($contract->award_type == 'fix') {
$as = (int)$contract->award_size;
if ($as > 1000000 or $as <= 0) {
$is_award_correct = false;
$message = 'Award size of fixed award must be between 0 and 1\'000\'000';
}
}

if ($contract->award_type == 'percent') {
$as = (int)$contract->award_size;
if ($as > 10 or $as <= 0) {
$is_award_correct = false;
$message = 'Award size of percent award must be between 0 and 10';
}
}

if (!$is_award_correct)
array_push($result, $message);
return $result;
}

private function check_sign_date(Contract $contract, ContractsRepository $contractsRepository, $result)
{
$isDateCorrect = true;
$expirationDate = new DateTime($contract->expiration_date);
$currentDate = new DateTime("now");

if ($expirationDate <= $currentDate)
array_push($result, 'Expiration date must be in future');
return $result;
}

private function check_expiration_date(Contract $contract, ContractsRepository $contractsRepository, $result)
{
return $result;
}
}
Loading