Completed
Push — master ( d2c9b2...2cde9b )
by
unknown
08:05 queued 04:31
created

AddEditAction::validates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2017, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Action\Collection;
13
14
/**
15
 * Class AddEditAction, uses POST and an array of entity data to either add or edit
16
 * Pass a not null primary key for edit, null for adding a new row
17
 * Validation done and using saveMany to store entities, atomic
18
 *
19
 * Example curl call
20
 *
21
 * curl --request POST --url http://example.com/api/posts/collection/add \
22
 *   --header 'content-type: multipart/form-data; boundary=---011000010111000001101001' \
23
 *   --form '0[id]=1' \
24
 *   --form '0[title]=Article1ForEdit' \
25
 *   --form '0[title]=NewArticle2'
26
 *
27
 * @package CakeDC\Api\Service\Action\Collection
28
 */
29
class AddEditAction extends CollectionAction
30
{
31
32
    /**
33
     * Apply validation process.
34
     *
35
     * @return bool
36
     */
37 3
    public function validates()
38
    {
39 3
        return $this->_validateMany();
40
    }
41
42
    /**
43
     * Execute action.
44
     *
45
     * @return mixed
46
     */
47 6
    public function execute()
48
    {
49 6
        $entities = $this->_newEntities([
50 6
            'accessibleFields' => [$this->getTable()->getPrimaryKey() => true
51 6
            ]]);
52
53 3
        return $this->_saveMany($entities);
54
    }
55
}
56