Test Failed
Branch add-core-tests (2d6874)
by Rafael
20:34 queued 11:07
created

ClearcacheTask::sessionsAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 10
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 24
ccs 0
cts 12
cp 0
crap 2
rs 9.9332
1
<?php
2
3
namespace Canvas\Cli\Tasks;
4
5
use function Canvas\Core\appPath;
6
use Phalcon\Cache\Backend\Libmemcached;
0 ignored issues
show
Bug introduced by
The type Phalcon\Cache\Backend\Libmemcached was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Phalcon\Cli\Task as PhTask;
0 ignored issues
show
Bug introduced by
The type Phalcon\Cli\Task was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use RecursiveDirectoryIterator;
9
use RecursiveIteratorIterator;
10
use Phalcon\Queue\Beanstalk\Extended as BeanstalkExtended;
11
use Phalcon\Queue\Beanstalk\Job;
0 ignored issues
show
Bug introduced by
The type Phalcon\Queue\Beanstalk\Job was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
/**
14
 * Class ClearcacheTask.
15
 *
16
 * @package Canvas\Cli\Tasks
17
 *
18
 * @property Libmemcached $cache
19
 * @property Config $config
20
 */
21
class ClearcacheTask extends PhTask
22
{
23
    /**
24
     * Clears the data cache from the application.
25
     */
26 1
    public function mainAction()
27
    {
28 1
        $this->clearFileCache();
29 1
        $this->clearMemCached();
30 1
    }
31
32
    /**
33
     * Clears file based cache.
34
     */
35 1
    protected function clearFileCache() : void
36
    {
37 1
        echo 'Clearing Cache folders' . PHP_EOL;
38
39 1
        $fileList = [];
40 1
        $whitelist = ['.', '..', '.gitignore'];
41 1
        $path = appPath('storage/cache');
42 1
        $dirIterator = new RecursiveDirectoryIterator($path);
43 1
        $iterator = new RecursiveIteratorIterator(
44 1
            $dirIterator,
45 1
            RecursiveIteratorIterator::CHILD_FIRST
46
        );
47
48
        /**
49
         * Get how many files we have there and where they are.
50
         */
51 1
        foreach ($iterator as $file) {
52 1
            if (true !== $file->isDir() && true !== in_array($file->getFilename(), $whitelist)) {
53 1
                $fileList[] = $file->getPathname();
54
            }
55
        }
56
57 1
        echo sprintf('Found %s files', count($fileList)) . PHP_EOL;
58 1
        foreach ($fileList as $file) {
59 1
            echo '.';
60 1
            unlink($file);
61
        }
62
63 1
        echo PHP_EOL . 'Cleared Cache folders' . PHP_EOL;
64 1
    }
65
66
    /**
67
     * Clears memcached data cache.
68
     */
69 1
    protected function clearMemCached() : void
70
    {
71 1
        echo 'Clearing data cache' . PHP_EOL;
72 1
        $options = $this->cache->getOptions();
73 1
        $servers = $options['servers'] ?? [];
74 1
        $memcached = new \Memcached();
75 1
        foreach ($servers as $server) {
76 1
            $memcached->addServer($server['host'], $server['port'], $server['weight']);
77
        }
78
79 1
        $keys = $memcached->getAllKeys();
80
        //print_r($keys);
81 1
        echo sprintf('Found %s keys', count($keys)) . PHP_EOL;
82 1
        foreach ($keys as $key) {
83
            if ('bakaapi-' === substr($key, 0, 8)) {
84
                $server = $memcached->getServerByKey($key);
85
                $result = $memcached->deleteByKey($server['host'], $key);
86
                $resultCode = $memcached->getResultCode();
87
                if (true === $result && $resultCode !== \Memcached::RES_NOTFOUND) {
88
                    echo '.';
89
                } else {
90
                    echo 'F';
91
                }
92
            }
93
        }
94
95 1
        echo  PHP_EOL . 'Cleared data cache' . PHP_EOL;
96 1
    }
97
98
    /**
99
     * Clean user session.
100
     *
101
     * @return void
102
     */
103
    public function sessionsAction() : void
104
    {
105
        //call queue
106
        $queue = new BeanstalkExtended([
107
            'host' => $this->config->beanstalk->host,
108
            'prefix' => $this->config->beanstalk->prefix,
109
        ]);
110
111
        //call que que tube
112
        $queue->addWorker(getenv('SESSION_QUEUE'), function (Job $job) {
113
            // Here we should collect the meta information, make the screenshots, convert the video to the FLV etc.
114
115
            $sessionId = $job->getBody();
116
            echo "\nProccessing:  {$sessionId}\n";
117
118
            $session = new \Baka\Auth\Models\Sessions();
119
            $session->clean($sessionId, true);
120
121
            // It's very important to send the right exit code!
122
            exit(0);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
123
        });
124
125
        // Start processing queues
126
        $queue->doWork();
127
    }
128
}
129