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

Entity::__invoke()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 6
nc 2
nop 3
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