Skip to content

Instantly share code, notes, and snippets.

@akrabat
Created December 7, 2016 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akrabat/9a40d5046f3cb9fa4e545efc51dd156f to your computer and use it in GitHub Desktop.
Save akrabat/9a40d5046f3cb9fa4e545efc51dd156f to your computer and use it in GitHub Desktop.
Playing with code
<?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