Created
December 7, 2016 16:09
-
-
Save akrabat/9a40d5046f3cb9fa4e545efc51dd156f to your computer and use it in GitHub Desktop.
Playing with code
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
<?php | |
class CustomerMaintenanceControllerFactory | |
{ | |
public function __invoke($c) { | |
$customerMapper = $c->get('CustomerMapper'); | |
$deleteForm = $c->get('CustomerDeleteForm'); | |
$editForm = $c->get('CustomerEditForm'); | |
return new CustomerMaintenanceController($customerMapper, $deleteForm, $editForm); | |
} | |
} | |
//----------------------------------------------------------------------------- | |
class CustomerMaintenanceController extends AbstractActionController | |
{ | |
public function __construct($customerMapper, $deleteForm, $editForm, $customerEmailer, $customerLogger, $customerSMSer, $customerAudtor) | |
{ | |
$this->customerMapper = $customerMapper; | |
$this->deleteForm = $deleteForm; | |
$this->editForm = $editForm; | |
} | |
public function listAction() { | |
// lists all customers | |
return new ViewModel([ | |
'customers' => $this->customerMapper->fetchAll(), | |
]); | |
} | |
public function viewAction() { | |
// views a single customer | |
$id = $this->params('id', 0); | |
$customer = $this->customerMapper->findById($id); | |
if (!$customer) { | |
return $this->redirect()->toRoute('customer/list'); | |
} | |
return new ViewModel([ | |
'customer' => $customer, | |
]); | |
} | |
public function editAction() { | |
// edits a customer | |
$form = $this->editForm; | |
if ($request->isPost()) { | |
$form->setData($request->getPost()); | |
if ($form->isValid()) { | |
$values = $form->getValues(); | |
if ($values['id'] == 0) { | |
$this->customerMapper->insert($values); | |
} else { | |
$this->customerMapper->update($values); | |
} | |
$this->redirect('customers/view/' . $values['id']); | |
} | |
} | |
return new ViewModel([ | |
'form' => $form, | |
]); | |
} | |
public function deleteAction() { | |
// deletes a customer | |
$form = $this->deleteForm; | |
if ($request->isPost()) { | |
$form->setData($request->getPost()); | |
if ($form->isValid()) { | |
$id = $form->getValue('id'); | |
$this->customerMapper->delete($id); | |
$this->redirect('customers/list'); | |
} | |
} | |
return new ViewModel([ | |
'form' => $form, | |
]); | |
} | |
} | |
// delete.phtml: form with yes and no buttons | |
echo PHP_EOL; | |
class CustomerMaintenanceControllerFactory { | |
public function __invoke($c) { | |
$logger = $c->get('Logger2'); | |
return new CustomerMaintenanceController($dForm, $eForm, $logger, $cService); | |
} | |
} | |
class CustomerMaintenanceController extends AbstractActionController | |
{ | |
public function __construct($deleteForm, $editForm, $logger, $customerService) | |
{ | |
$logger->log('My message'); | |
} | |
} | |
class CustomerService | |
{ | |
public function __construct($customerMapper, $customerAuditor) { | |
} | |
public function saveCustomer($data) | |
{ | |
$customer = $this->customerMapper->save($data); | |
$this->events->trigger("saveCustomer", $this, ['customer' => $customer]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment