Completed
Pull Request — master (#40)
by
unknown
10:59
created

CollectionAction   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 84
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A _validateMany() 0 23 3
A _saveMany() 0 11 2
A _newEntities() 0 13 1
A _validateDataIsArray() 0 6 3
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
use CakeDC\Api\Exception\ValidationException;
15
use CakeDC\Api\Service\Action\CrudAction;
16
use Cake\Utility\Hash;
17
18
/**
19
 * Class CollectionAction
20
 *
21
 * @package CakeDC\Api\Service\Action\Collection
22
 */
23
abstract class CollectionAction extends CrudAction
24
{
25
26
    /**
27
     * Apply validation process to many entities
28
     *
29
     * @return bool
30
     */
31 3
    protected function _validateMany()
32
    {
33 3
        $validator = $this->getTable()->validator();
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Validation\ValidatorAwareTrait::validator() has been deprecated with message: 3.5.0 Use getValidator/setValidator instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
34 3
        $datas = $this->data();
35 3
        $this->_validateDataIsArray($datas);
36 3
        $index = 0;
37
        $errors = collection($datas)->reduce(function ($errors, $data) use ($validator, &$index) {
38 3
            $error = $validator->errors($data);
39 3
            if ($error) {
40 1
                $errors[$index] = $error;
41 1
            }
42
43 3
            $index++;
44
45 3
            return $errors;
46 3
        }, []);
47
48 3
        if (!empty($errors)) {
49 1
            throw new ValidationException(__('Validation failed'), 0, null, $errors);
50
        }
51
52 2
        return true;
53
    }
54
55
    /**
56
     * Save many entities
57
     *
58
     * @param array $entities entities
59
     * @return array of entities saved
60
     */
61 3
    protected function _saveMany($entities = [])
62
    {
63 3
        if ($this->getTable()->saveMany($entities)) {
64 2
            return $entities;
65
        } else {
66
            $errors = collection($entities)->reduce(function ($errors, $entity) {
67 1
                $errors[] = $entity->errors();
68 1
            }, []);
69 1
            throw new ValidationException(__('Validation on {0} failed', $this->getTable()->getAlias()), 0, null, $errors);
70
        }
71
    }
72
73
    /**
74
     * Create entities from the posted data
75
     *
76
     * @param array $patchOptions options to use un patch
77
     * @return array entities
78
     */
79 11
    protected function _newEntities($patchOptions = [])
80
    {
81 11
        $datas = $this->data();
82 11
        $this->_validateDataIsArray($datas);
83
84 5
        return collection($datas)->reduce(function ($entities, $data) use ($patchOptions) {
85 5
            $entity = $this->_newEntity();
86 5
            $entity = $this->_patchEntity($entity, $data, $patchOptions);
87 5
            $entities[] = $entity;
88
89 5
            return $entities;
90 5
        }, []);
91
    }
92
93
    /**
94
     * Ensure the data is a not empty array
95
     *
96
     * @param mixed $datas posted data
97
     * @throws ValidationException
98
     * @return void
99
     */
100 15
    protected function _validateDataIsArray($datas)
101
    {
102 15
        if (!is_array($datas) || Hash::dimensions($datas) < 2) {
103 6
            throw new ValidationException(__('Validation failed, POST data is not an array of items'), 0, null);
104
        }
105 9
    }
106
}
107