ImapIdleCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 31
c 1
b 0
f 1
dl 0
loc 87
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
B handle() 0 35 6
A onNewMessage() 0 2 1
1
<?php
2
3
namespace Webklex\IMAP\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Log;
7
use Webklex\IMAP\Facades\Client as ClientFacade;
8
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
9
use Webklex\PHPIMAP\Exceptions\FolderFetchingException;
10
use Webklex\PHPIMAP\Folder;
11
use Webklex\PHPIMAP\Message;
12
13
class ImapIdleCommand extends Command {
14
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'imap:idle';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Fetch new messages by utilising imap idle';
28
29
    /**
30
     * Holds the account information
31
     *
32
     * @var string|array $account
33
     */
34
    protected $account = "default";
35
36
    /**
37
     * Name of the used folder
38
     *
39
     * @var string $folder_name
40
     */
41
    protected $folder_name = "INBOX";
42
43
    /**
44
     * Create a new command instance.
45
     *
46
     * @return void
47
     */
48
    public function __construct() {
49
        parent::__construct();
50
    }
51
52
    /**
53
     * Callback used for the idle command and triggered for every new received message
54
     * @param Message $message
55
     */
56
    public function onNewMessage(Message $message){
57
        $this->info("New message received: ".$message->subject);
58
    }
59
60
    /**
61
     * Execute the console command.
62
     *
63
     * @return int
64
     */
65
    public function handle() {
66
        if (is_array($this->account)) {
67
            $client = ClientFacade::make($this->account);
0 ignored issues
show
Bug Best Practice introduced by
The method Webklex\IMAP\Facades\Client::make() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            /** @scrutinizer ignore-call */ 
68
            $client = ClientFacade::make($this->account);
Loading history...
68
        }else{
69
            $client = ClientFacade::account($this->account);
0 ignored issues
show
Bug Best Practice introduced by
The method Webklex\IMAP\Facades\Client::account() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            /** @scrutinizer ignore-call */ 
70
            $client = ClientFacade::account($this->account);
Loading history...
70
        }
71
72
        try {
73
            $client->connect();
74
        } catch (ConnectionFailedException $e) {
75
            Log::error($e->getMessage());
76
            return 1;
77
        }
78
79
        /** @var Folder $folder */
80
        try {
81
            $folder = $client->getFolder($this->folder_name);
82
        } catch (ConnectionFailedException $e) {
83
            Log::error($e->getMessage());
84
            return 1;
85
        } catch (FolderFetchingException $e) {
86
            Log::error($e->getMessage());
87
            return 1;
88
        }
89
90
        try {
91
            $folder->idle(function($message){
92
                $this->onNewMessage($message);
93
            });
94
        } catch (\Exception $e) {
95
            Log::error($e->getMessage());
96
            return 1;
97
        }
98
99
        return 0;
100
    }
101
}
102