Total Complexity | 42 |
Total Lines | 362 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Backfill often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Backfill, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Backfill |
||
10 | { |
||
11 | /** |
||
12 | * @var \Blacklight\db\DB |
||
13 | */ |
||
14 | public $pdo; |
||
15 | |||
16 | /** |
||
17 | * @var |
||
18 | */ |
||
19 | protected $_binaries; |
||
20 | |||
21 | /** |
||
22 | * @var \Blacklight\NNTP |
||
23 | */ |
||
24 | protected $_nntp; |
||
25 | /** |
||
26 | * Should we use compression for headers? |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | protected $_compressedHeaders; |
||
31 | |||
32 | /** |
||
33 | * Log and or echo debug. |
||
34 | * @var bool |
||
35 | */ |
||
36 | protected $_debug = false; |
||
37 | |||
38 | /** |
||
39 | * Echo to cli? |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $_echoCLI; |
||
43 | |||
44 | /** |
||
45 | * How far back should we go on safe back fill? |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $_safeBackFillDate; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $_safePartRepair; |
||
55 | |||
56 | /** |
||
57 | * Should we disable the group if we have backfilled far enough? |
||
58 | * @var bool |
||
59 | */ |
||
60 | protected $_disableBackfillGroup; |
||
61 | |||
62 | /** |
||
63 | * Constructor. |
||
64 | * |
||
65 | * @param array $options Class instances / Echo to cli? |
||
66 | * |
||
67 | * @throws \Exception |
||
68 | */ |
||
69 | public function __construct(array $options = []) |
||
70 | { |
||
71 | $defaults = [ |
||
72 | 'Echo' => true, |
||
73 | 'Logger' => null, |
||
74 | 'Groups' => null, |
||
75 | 'NNTP' => null, |
||
76 | 'Settings' => null, |
||
77 | ]; |
||
78 | $options += $defaults; |
||
79 | |||
80 | $this->_echoCLI = ($options['Echo'] && config('nntmux.echocli')); |
||
81 | |||
82 | $this->pdo = ($options['Settings'] instanceof DB ? $options['Settings'] : new DB()); |
||
83 | $this->_nntp = ( |
||
84 | $options['NNTP'] instanceof NNTP |
||
85 | ? $options['NNTP'] : new NNTP(['Settings' => $this->pdo]) |
||
86 | ); |
||
87 | |||
88 | $this->_compressedHeaders = (int) Settings::settingValue('..compressedheaders') === 1; |
||
|
|||
89 | $this->_safeBackFillDate = Settings::settingValue('..safebackfilldate') !== '' ? (string) Settings::settingValue('safebackfilldate') : '2008-08-14'; |
||
90 | $this->_safePartRepair = (int) Settings::settingValue('..safepartrepair') === 1 ? 'update' : 'backfill'; |
||
91 | $this->_disableBackfillGroup = (int) Settings::settingValue('..disablebackfillgroup') === 1; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Backfill all the groups up to user specified time/date. |
||
96 | * |
||
97 | * @param string $groupName |
||
98 | * @param string|int $articles |
||
99 | * @param string $type |
||
100 | * |
||
101 | * @return void |
||
102 | * @throws \Exception |
||
103 | */ |
||
104 | public function backfillAllGroups($groupName = '', $articles = '', $type = ''): void |
||
105 | { |
||
106 | $res = []; |
||
107 | if ($groupName !== '') { |
||
108 | $grp = Group::getByName($groupName); |
||
109 | if ($grp) { |
||
110 | $res = [$grp]; |
||
111 | } |
||
112 | } else { |
||
113 | $res = Group::getActiveBackfill($type); |
||
114 | } |
||
115 | |||
116 | $groupCount = count($res); |
||
117 | if ($groupCount > 0) { |
||
118 | $counter = 1; |
||
119 | $allTime = microtime(true); |
||
120 | $dMessage = ( |
||
121 | 'Backfilling: '. |
||
122 | $groupCount. |
||
123 | ' group(s) - Using compression? '. |
||
124 | ($this->_compressedHeaders ? 'Yes' : 'No') |
||
125 | ); |
||
126 | |||
127 | if ($this->_echoCLI) { |
||
128 | ColorCLI::doEcho(ColorCLI::header($dMessage), true); |
||
129 | } |
||
130 | |||
131 | $this->_binaries = new Binaries( |
||
132 | ['NNTP' => $this->_nntp, 'Echo' => $this->_echoCLI, 'Settings' => $this->pdo] |
||
133 | ); |
||
134 | |||
135 | if ($articles !== '' && ! is_numeric($articles)) { |
||
136 | $articles = 20000; |
||
137 | } |
||
138 | |||
139 | // Loop through groups. |
||
140 | foreach ($res as $groupArr) { |
||
141 | if ($groupName === '') { |
||
142 | $dMessage = 'Starting group '.$counter.' of '.$groupCount; |
||
143 | |||
144 | if ($this->_echoCLI) { |
||
145 | ColorCLI::doEcho(ColorCLI::header($dMessage), true); |
||
146 | } |
||
147 | } |
||
148 | $this->backfillGroup($groupArr, $groupCount - $counter, $articles); |
||
149 | $counter++; |
||
150 | } |
||
151 | |||
152 | $dMessage = 'Backfilling completed in '.number_format(microtime(true) - $allTime, 2).' seconds.'; |
||
153 | |||
154 | if ($this->_echoCLI) { |
||
155 | ColorCLI::doEcho(ColorCLI::primary($dMessage), true); |
||
156 | } |
||
157 | } else { |
||
158 | $dMessage = 'No groups specified. Ensure groups are added to database for updating.'; |
||
159 | |||
160 | if ($this->_echoCLI) { |
||
161 | ColorCLI::doEcho(ColorCLI::warning($dMessage), true); |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Backfill single group. |
||
168 | * |
||
169 | * @param array $groupArr |
||
170 | * @param int $left |
||
171 | * @param int|string $articles |
||
172 | * |
||
173 | * @return void |
||
174 | * @throws \Exception |
||
175 | */ |
||
176 | public function backfillGroup($groupArr, $left, $articles = ''): void |
||
337 | ); |
||
338 | } |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Safe backfill using posts. Going back to a date specified by the user on the site settings. |
||
343 | * This does 1 group for x amount of parts until it reaches the date. |
||
344 | * |
||
345 | * @param string $articles |
||
346 | * |
||
347 | * @return void |
||
348 | * @throws \Exception |
||
349 | */ |
||
350 | public function safeBackfill($articles = ''): void |
||
371 | } |
||
372 | } |
||
373 |