Completed
Branch CASC/base (79f9d1)
by
unknown
16:50 queued 08:50
created

NodeGroupDao::persistModelObjNodesGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\orm\tree_traversal;
4
5
use EE_Error;
6
use EventEspresso\core\exceptions\InvalidDataTypeException;
7
use EventEspresso\core\exceptions\InvalidInterfaceException;
8
use EventEspresso\core\exceptions\UnexpectedEntityException;
9
use Exception;
10
use InvalidArgumentException;
11
use ReflectionException;
12
13
/**
14
 * Class NodeGroupDao
15
 *
16
 * Used to store, retrieve, and delete a group of ModelObjNodes.
17
 *
18
 * @package     Event Espresso
19
 * @author         Mike Nelson
20
 * @since         $VID:$
21
 *
22
 */
23
class NodeGroupDao
24
{
25
    /**
26
     * @since $VID:$
27
     * @return mixed|void
28
     */
29
    public function generateGroupCode()
30
    {
31
        return wp_generate_password(6, false);
32
    }
33
34
    /**
35
     * Gets the string we put in front of the WP Option name used to store the jobs.
36
     * @since $VID:$
37
     * @return string
38
     */
39
    private function getOptionPrefix()
40
    {
41
        return 'ee_deletion_';
42
    }
43
44
    /**
45
     * @since $VID:$
46
     * @param $code
47
     * @return ModelObjNode[]
48
     * @throws UnexpectedEntityException
49
     */
50
    public function getModelObjNodesInGroup($code)
51
    {
52
        if (! $code) {
53
            throw new Exception(esc_html__('We aren’t sure which job you are performing. Please press back in your browser and try again.', 'event_espresso'));
54
        }
55
        $deletion_data = get_option($this->getOptionPrefix() . $code, []);
56
        foreach ($deletion_data as $root) {
57
            if (! $root instanceof ModelObjNode) {
58
                throw new UnexpectedEntityException($root, 'ModelObjNode');
59
            }
60
        }
61
        return $deletion_data;
62
    }
63
64
    /**
65
     * Gets an array indicating what database rows are contained in the job.
66
     * Each top-level key is a model name, and its value is an array of IDs.
67
     * @since $VID:$
68
     * @param ModelObjNode[] $model_obj_nodes
69
     * @return array
70
     * @throws EE_Error
71
     * @throws InvalidDataTypeException
72
     * @throws InvalidInterfaceException
73
     * @throws InvalidArgumentException
74
     * @throws ReflectionException
75
     */
76
    public function getModelsAndIdsContainedIn($model_obj_nodes)
77
    {
78
        $models_and_ids_to_delete = [];
79
        foreach ($model_obj_nodes as $root) {
80
            $models_and_ids_to_delete = array_replace_recursive($models_and_ids_to_delete, $root->getIds());
81
        }
82
        return $models_and_ids_to_delete;
83
    }
84
85
    /**
86
     * Gets an array indicating what database rows are contained in the job.
87
     * Each top-level key is a model name, and its value is an array of IDs.
88
     * @since $VID:$
89
     * @param string $code
90
     * @return array
91
     * @throws EE_Error
92
     * @throws InvalidArgumentException
93
     * @throws InvalidDataTypeException
94
     * @throws InvalidInterfaceException
95
     * @throws ReflectionException
96
     * @throws UnexpectedEntityException
97
     */
98
    public function getModelsAndIdsFromGroup($code)
99
    {
100
        $model_obj_nodes = $this->getModelObjNodesInGroup($code);
101
        return $this->getModelsAndIdsContainedIn($model_obj_nodes);
102
    }
103
104
    /**
105
     * Persists the ModelObjNodes for future requests, using the code for reference.
106
     * @since $VID:$
107
     * @param ModelObjNode[] $model_obj_nodes
108
     * @param string $code
109
     * @return bool
110
     */
111
    public function persistModelObjNodesGroup($model_obj_nodes, $code)
112
    {
113
        return add_option(
114
            $this->getOptionPrefix() . $code,
115
            $model_obj_nodes,
116
            null,
117
            'no'
118
        );
119
    }
120
121
    /**
122
     * Forgets about the group of ModelObjNodes. Doesn't delete the rows in the database they reference though.
123
     * @since $VID:$
124
     * @param $code
125
     * @return bool
126
     */
127
    public function deleteModelObjNodesInGroup($code)
128
    {
129
        return delete_option($this->getOptionPrefix() . $code);
130
    }
131
}
132
// End of file NodeGroupDao.php
133
// Location: EventEspresso\core\services\orm\tree_traversal/NodeGroupDao.php
134