Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 70 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CampaignLog | |
0.00% |
0 / 70 |
|
0.00% |
0 / 2 |
702 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
42 | |||
changes | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
420 |
1 | <?php |
2 | |
3 | class CampaignLog { |
4 | /** @var string[] */ |
5 | private static $basic_fields = [ |
6 | 'start', 'end', 'enabled', 'preferred', 'locked', 'geo', 'buckets' |
7 | ]; |
8 | /** @var string[] */ |
9 | private static $list_fields = [ 'projects', 'languages', 'countries', 'regions' ]; |
10 | /** @var string[] */ |
11 | private static $map_fields = [ 'banners' ]; |
12 | |
13 | /** @var mixed[] */ |
14 | private $begin; |
15 | /** @var mixed[] */ |
16 | private $end; |
17 | /** @var string */ |
18 | private $campaign; |
19 | /** @var string */ |
20 | private $action; |
21 | /** @var string|int */ |
22 | private $timestamp; |
23 | /** @var string|null */ |
24 | private $comment; |
25 | |
26 | /** |
27 | * @param stdClass|null $row |
28 | */ |
29 | public function __construct( $row = null ) { |
30 | $this->begin = []; |
31 | $this->end = []; |
32 | if ( $row ) { |
33 | $comma_explode = static function ( $str ) { |
34 | return explode( ", ", $str ); |
35 | }; |
36 | |
37 | $json_decode = static function ( $json ) { |
38 | return json_decode( $json, true ); |
39 | }; |
40 | |
41 | $store = function ( $name, $decode = null ) use ( $row ) { |
42 | $beginField = 'notlog_begin_' . $name; |
43 | $endField = 'notlog_end_' . $name; |
44 | |
45 | if ( !$decode ) { |
46 | $decode = static function ( $v ) { |
47 | return $v; |
48 | }; |
49 | } |
50 | $this->begin[ $name ] = $decode( $row->$beginField ); |
51 | $this->end[ $name ] = $decode( $row->$endField ); |
52 | }; |
53 | |
54 | foreach ( static::$basic_fields as $name ) { |
55 | $store( $name ); |
56 | } |
57 | foreach ( static::$list_fields as $name ) { |
58 | $store( $name, $comma_explode ); |
59 | } |
60 | foreach ( static::$map_fields as $name ) { |
61 | $store( $name, $json_decode ); |
62 | } |
63 | } |
64 | |
65 | $this->campaign = $row->notlog_not_name; |
66 | $this->action = $row->notlog_action; |
67 | $this->timestamp = $row->notlog_timestamp; |
68 | // TODO temporary code for soft dependency on schema change |
69 | $this->comment = $row->notlog_comment ?? ''; |
70 | } |
71 | |
72 | /** |
73 | * TODO: Use in {@see LogPager} |
74 | * @return array<string,array> |
75 | */ |
76 | public function changes() { |
77 | $removed = []; |
78 | $added = []; |
79 | |
80 | $diff_basic = function ( $name ) use ( &$removed, &$added ) { |
81 | if ( $this->begin[ $name ] !== $this->end[ $name ] ) { |
82 | if ( $this->begin[ $name ] !== null ) { |
83 | $removed[ $name ] = $this->begin[ $name ]; |
84 | } |
85 | if ( $this->end[ $name ] !== null ) { |
86 | $added[ $name ] = $this->end[ $name ]; |
87 | } |
88 | } |
89 | }; |
90 | $diff_list = function ( $name ) use ( &$removed, &$added ) { |
91 | if ( $this->begin[ $name ] !== $this->end[ $name ] ) { |
92 | $removed[ $name ] = array_diff( $this->begin[ $name ], $this->end[ $name ] ); |
93 | if ( !$removed[ $name ] || $removed[ $name ] === [ "" ] ) { |
94 | unset( $removed[ $name ] ); |
95 | } |
96 | $added[ $name ] = array_diff( $this->end[ $name ], $this->begin[ $name ] ); |
97 | if ( !$added[ $name ] || $added[ $name ] === [ "" ] ) { |
98 | unset( $added[ $name ] ); |
99 | } |
100 | } |
101 | }; |
102 | $diff_map = function ( $name ) use ( &$removed, &$added ) { |
103 | $removed[ $name ] = $this->begin[ $name ]; |
104 | $added[ $name ] = $this->end[ $name ]; |
105 | |
106 | if ( $this->begin[ $name ] && $this->end[ $name ] ) { |
107 | $all_keys = array_keys( array_merge( $this->begin[ $name ], $this->end[ $name ] ) ); |
108 | foreach ( $all_keys as $item ) { |
109 | # simplification: match contents, but diff at item level |
110 | if ( array_key_exists( $item, $this->begin[ $name ] ) |
111 | && array_key_exists( $item, $this->end[ $name ] ) |
112 | && $added[ $name ][ $item ] === $removed[ $name ][ $item ] |
113 | ) { |
114 | unset( $added[ $name ][ $item ] ); |
115 | unset( $removed[ $name ][ $item ] ); |
116 | } |
117 | } |
118 | } |
119 | if ( !$removed[ $name ] ) { |
120 | unset( $removed[ $name ] ); |
121 | } |
122 | if ( !$added[ $name ] ) { |
123 | unset( $added[ $name ] ); |
124 | } |
125 | }; |
126 | foreach ( static::$basic_fields as $name ) { |
127 | $diff_basic( $name ); |
128 | } |
129 | foreach ( static::$list_fields as $name ) { |
130 | $diff_list( $name ); |
131 | } |
132 | foreach ( static::$map_fields as $name ) { |
133 | $diff_map( $name ); |
134 | } |
135 | |
136 | return [ 'removed' => $removed, 'added' => $added ]; |
137 | } |
138 | } |