• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmvc-plugin / controller / 4220e5ab-8da0-46ab-ac63-9d340cd1c04f

09 Sep 2023 06:57AM UTC coverage: 63.089% (+0.1%) from 62.987%
4220e5ab-8da0-46ab-ac63-9d340cd1c04f

push

circleci

web-flow
Merge pull request #39 from pmvc-plugin/analysis-22M3Gl

Apply fixes from StyleCI

61 of 61 new or added lines in 3 files covered. (100.0%)

388 of 615 relevant lines covered (63.09%)

3.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/_process_worker.php
1
<?php
2
/**
3
 * PMVC.
4
 *
5
 * PHP version 8
6
 *
7
 * @category Worker
8
 *
9
 * @package PMVC
10
 *
11
 * @author  Hill <hill@kimo.com>
12
 * @license http://opensource.org/licenses/MIT MIT
13
 *
14
 * @version GIT: <git_id>
15
 *
16
 * @link https://packagist.org/packages/pmvc/pmvc
17
 */
18

19
namespace PMVC;
20

21
use PMVC\PlugIn\supervisor as sup;
22
use PMVC\PlugIn\supervisor\Parallel;
23

24
/*
25
 * Process Worker.
26
 *
27
 * @category Worker
28
 *
29
 * @package PMVC
30
 *
31
 * @author  Hill <hill@kimo.com>
32
 * @license http://opensource.org/licenses/MIT MIT
33
 *
34
 * @link https://packagist.org/packages/pmvc/pmvc
35
 */
36

37
const TASK_KEY = 'PMVC\Task';
×
38
const QUEUE_KEY = 'PMVC\Queue';
×
39

40
// @codingStandardsIgnoreStart
41
${_INIT_CONFIG}[_CLASS] = __NAMESPACE__.'\process_worker';
×
42
class process_worker // @codingStandardsIgnoreEnd
43
{
44
    public $caller;
45

46
    /**
47
     * Porcess worker invoke.
48
     *
49
     * @return ActionForward
50
     */
51
    public function __invoke()
52
    {
53
        $annotation = \PMVC\plug('annotation');
×
54
        $supervisor = \PMVC\plug('supervisor');
×
55
        $caller = $this->caller;
×
56
        $mappings = $caller->getMappings();
×
57
        $keys = $mappings->keySet();
×
58
        foreach ($keys as $key) {
×
59
            $action = $mappings->findAction($key);
×
60
            $form = $caller->processForm($action);
×
61
            $func = $caller->getActionFunc($action);
×
62
            $attrs = $annotation->getAttrs($func);
×
63
            $taskAttr = \PMVC\get($attrs['obj'], TASK_KEY);
×
64
            $queueAttr = \PMVC\get($attrs['obj'], QUEUE_KEY);
×
65
            if ($taskAttr) {
×
66
                $wrap = function () use (
×
67
                    $action,
×
68
                    $form,
×
69
                    $queueAttr,
×
70
                    $func
×
71
                ) {
×
72
                    $queueModel = $this->_getQueueModel($queueAttr);
×
73
                    if ($queueAttr && $queueAttr->consumer) {
×
74
                        $form['data'] = $queueModel[null];
×
75
                    }
76
                    $result = call_user_func_array($func, [$action, $form]);
×
77
                    if ($queueAttr && $queueAttr->publisher && $result['ok']) {
×
78
                        $queueModel[] = $result['data'];
×
79
                    }
80
                };
×
81
                switch ($taskAttr->type) {
×
82
                    case sup\TYPE_DAEMON:
×
83
                        $workerGroup = $taskAttr->group;
×
84
                        $inputConcurrency = get($form, $workerGroup);
×
85
                        $concurrency = !empty($inputConcurrency) &&
×
86
                            is_numeric($inputConcurrency) &&
×
87
                            $inputConcurrency > 1
×
88
                                ? $form[$workerGroup]
×
89
                                : 1;
×
90
                        for ($i = 0; $i < $concurrency; $i++) {
×
91
                            new Parallel(
×
92
                                $wrap,
×
93
                                [
×
94
                                    sup\TYPE     => sup\TYPE_DAEMON,
×
95
                                    sup\INTERVAL => $taskAttr->interval,
×
96
                                ]
×
97
                            );
×
98
                        }
99
                        break;
×
100
                    case sup\TYPE_SCRIPT:
×
101
                        new Parallel(
×
102
                            $wrap,
×
103
                            [
×
104
                                sup\TYPE => sup\TYPE_SCRIPT,
×
105
                                sup\NAME => $action->name,
×
106
                            ]
×
107
                        );
×
108
                        break;
×
109
                    default:
110
                        trigger_error(
×
111
                            'Wrong worker type ['.$taskAttr->type.']'
×
112
                        );
×
113
                        break;
×
114
                }
115
            }
116
        }
117
        $supervisor->process();
×
118
    }
119

120
    /**
121
     * Get queue model.
122
     *
123
     * @param object $queueAttr Queue parameters.
124
     *
125
     * @return mixed model object
126
     */
127
    private function _getQueueModel($queueAttr)
128
    {
129
        $amqp = \PMVC\plug('amqp', ['host' => 'rabbitmq']);
×
130
        $queueModel = null;
×
131
        if ($queueAttr) {
×
132
            $queueModel = $amqp->getModel($queueAttr->name);
×
133
        }
134

135
        return $queueModel;
×
136
    }
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc