Completed
Push — master ( 5cd95a...6f73c5 )
by Oscar
03:28
created

Entity   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 8
Bugs 2 Features 1
Metric Value
wmc 5
c 8
b 2
f 1
lcom 0
cbo 5
dl 0
loc 40
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 11 3
A createForm() 0 16 2
1
<?php
2
3
namespace Folk\Controllers;
4
5
use Psr\Http\Message\ServerRequestInterface as Request;
6
use Psr\Http\Message\ResponseInterface as Response;
7
use Folk\Admin;
8
use Folk\Entities\EntityInterface;
9
use Psr7Middlewares\Middleware\FormatNegotiator;
10
use FormManager\Builder as F;
11
12
abstract class Entity
13
{
14
    public function __invoke(Request $request, Response $response, Admin $app)
15
    {
16
        $format = FormatNegotiator::getFormat($request);
17
        $entity = $app->getEntity($request->getAttribute('entity'));
18
19
        if ($entity !== null && method_exists($this, $format)) {
20
            return $this->$format($request, $response, $app, $entity);
21
        }
22
23
        return $response->withStatus(404);
24
    }
25
26
    /**
27
     * Helper to build the entity form.
28
     * 
29
     * @param EntityInterface $entity
30
     * @param Admin           $app
31
     * @param mixed|null      $id
32
     * 
33
     * return \Folk\Formats\Form
34
     */
35
    protected static function createForm(EntityInterface $entity, Admin $app, $id = null)
36
    {
37
        $form = F::form()
38
            ->method('post')
39
            ->enctype('multipart/form-data')
40
            ->add([
41
                'entity' => F::hidden()->val($entity->getName())->class('field-data-entity'),
42
                'data' => $entity->getScheme($app['builder']),
43
            ]);
44
45
        if ($id !== null) {
46
            $form['id'] = F::hidden()->val($id)->class('field-data-id');
47
        }
48
49
        return $form;
50
    }
51
}
52