Debian Codegraph @

classes/Sources/SourceAction.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2022-2023 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This file contains the class SourceAction
25 *
26 * @category API
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 namespace Liuch\DmarcSrg\Sources;
33
34 use Liuch\DmarcSrg\Exception\LogicException;
35
36 /**
37 * It's a class for describing one action of source
38 */
39 class SourceAction
40 {
41 public const ACTION_SEEN = 1;
42 public const ACTION_MOVE = 2;
43 public const ACTION_DELETE = 3;
44 public const FLAG_BASENAME = 1;
45
46 private $valid = false;
47 private $type = 0;
48 private $param = null;
49
50 /**
51 * The constructor
52 *
53 * @param string $action Action name with parameter separated by colon
54 * Examples: 'move_to:failed', 'delete'
55 *
56 * @return void
57 */
58 private function __construct(string $action)
59 {
60 if (($delim_offset = mb_strpos($action, ':')) === false) {
61 $name = $action;
62 $param = null;
63 } else {
64 $name = mb_substr($action, 0, $delim_offset);
65 $param = mb_substr($action, $delim_offset + 1);
66 }
67 switch ($name) {
68 case 'mark_seen':
69 $this->type = self::ACTION_SEEN;
70 if (!empty($param)) {
71 return;
72 }
73 break;
74 case 'move_to':
75 $this->type = self::ACTION_MOVE;
76 if (empty($param)) {
77 return;
78 }
79 break;
80 case 'delete':
81 $this->type = self::ACTION_DELETE;
82 if (!empty($param)) {
83 return;
84 }
85 break;
86 default:
87 return;
88 }
89 $this->param = $param;
90 $this->valid = true;
91 }
92
93 /**
94 * The getter
95 *
96 * @param string $name Property name. Must be one of the following: 'type', 'param'
97 *
98 * @return mixed
99 */
100 public function __get(string $name)
101 {
102 if (in_array($name, [ 'type', 'param' ])) {
103 return $this->$name;
104 }
105 throw new LogicException('Undefined property: ' . $name);
106 }
107
108 /**
109 * Handles a setting, flags, and returns an array of SourceAction instances
110 *
111 * @param string|array $setting Setting from the conf.php
112 * @param int $flags Flags of extra checking the result
113 * @param string $default Action to add if the result array is empty
114 *
115 * @return array
116 */
117 public static function fromSetting($setting, int $flags, string $default): array
118 {
119 if (gettype($setting) !== 'array') {
120 $setting = [ $setting ];
121 }
122 $tmap = [];
123 $list = [];
124 foreach ($setting as $it) {
125 if (gettype($it) === 'string') {
126 $sa = new self($it);
127 if ($sa->valid && !isset($tmap[$sa->type])) {
128 if (($flags & self::FLAG_BASENAME) && !self::checkBasename($sa)) {
129 continue;
130 }
131 $list[] = $sa;
132 $tmap[$sa->type] = true;
133 }
134 }
135 }
136 if (count($list) === 0) {
137 $sa = new self($default);
138 if ($sa->valid) {
139 $list[] = $sa;
140 }
141 }
142 return $list;
143 }
144
145 /**
146 * Checks if the param is just a directory name without a path
147 *
148 * @param self $sa
149 *
150 * @return bool
151 */
152 private static function checkBasename($sa): bool
153 {
154 return ($sa->type !== self::ACTION_MOVE || basename($sa->param) === $sa->param);
155 }
156 }