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

grueneschweiz / mailchimpservice / 17129055143

21 Aug 2025 01:54PM UTC coverage: 68.62% (+0.02%) from 68.598%
17129055143

push

github

Michael-Schaer
Improved debugging for linux and vscode

960 of 1399 relevant lines covered (68.62%)

11.03 hits per line

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

1.92
/app/Console/Commands/Sync.php
1
<?php
2

3
namespace App\Console\Commands;
4

5
use App\Exceptions\ConfigException;
6
use App\Exceptions\ParseCrmDataException;
7
use App\Synchronizer\CrmToMailchimpSynchronizer;
8
use App\Synchronizer\MailchimpToCrmCronSynchronizer;
9
use Illuminate\Console\Command;
10

11
class Sync extends Command
12
{
13
    private const DIRECTION_MAILCHIMP = 'toMailchimp';
14
    private const DIRECTION_CRM = 'toCrm';
15

16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'sync:all
22
                            {direction : Possible values are "' . self::DIRECTION_CRM . '" and "' . self::DIRECTION_MAILCHIMP . '".}
23
                            {config : The name of the config file to use.}
24
                            {--limit=100 : How may records should be synchronized at a time.}
25
                            {--offset=0 : How many records should be skipped. Usually used in combination with --limit.}
26
                            {--max=0 : How many records should be synchronized in total.}
27
                            {--all : Ignore revision and sync all records, not just changes.}
28
                            {--force : Ignore locks of previously started (running or dead) sync processes.}';
29

30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Synchronize all relevant records from the crm to mailchimp and vice versa.';
36

37
    /**
38
     * Create a new command instance.
39
     *
40
     * @return void
41
     */
42
    public function __construct()
43
    {
44
        parent::__construct();
1✔
45
    }
46

47
    /**
48
     * Execute the console command.
49
     *
50
     * @return integer
51
     *
52
     * @throws \Exception
53
     */
54
    public function handle()
55
    {
56
        switch ($this->argument('direction')) {
×
57
            case self::DIRECTION_CRM:
58
                return $this->syncToCrm();
×
59
            case self::DIRECTION_MAILCHIMP:
60
                return $this->syncToMailchimp();
×
61
            default:
62
                $this->error('Invalid direction argument.');
×
63
                $this->info("Possible values are:\n - " . self::DIRECTION_MAILCHIMP . "\n - " . self::DIRECTION_CRM);
×
64

65
                return 1;
×
66
        }
67
    }
68

69
    /**
70
     * Sync changes from mailchimp to the crm
71
     *
72
     * @return int
73
     * 
74
     * @throws \Exception
75
     */
76
    private function syncToCrm()
77
    {
78
        $limit = $this->option('limit');
×
79
        $max = $this->option('max');
×
80

81
        if (!is_numeric($limit) || (int)$limit <= 0) {
×
82
            $this->error('The limit option must pass an integer > 0.');
×
83

84
            return 1;
×
85
        }
86

87
        if (!is_numeric($max) || (int)$max < 0) {
×
88
            $this->error('The max option must pass an integer >= 0.');
×
89

90
            return 1;
×
91
        }
92

93
        try {
94
            $sync = new MailchimpToCrmCronSynchronizer($this->argument('config'));
×
95

96
            $this->info('Starting Mailchimp to CRM synchronization...');
×
97
            $result = $sync->syncAll((int)$limit, (int)$max);
×
98
            $this->info("Synchronization completed: {$result['processed']} processed, {$result['success']} successful, {$result['failed']} failed");
×
99

100
            return 0;
×
101
        } catch (\App\Exceptions\ConfigException $e) {
×
102
            $this->error('Configuration error: ' . $e->getMessage());
×
103
            return 1;
×
104
        } catch (\Exception $e) {
×
105
            $this->error('Error during synchronization: ' . $e->getMessage());
×
106
            return 1;
×
107
        }
108
    }
109

110
    /**
111
     * Sync crm records to mailchimp
112
     *
113
     * @return int
114
     *
115
     * @throws \Exception
116
     */
117
    private function syncToMailchimp()
118
    {
119
        $limit = $this->option('limit');
×
120
        $offset = $this->option('offset');
×
121
        $all = $this->option('all');
×
122
        $force = $this->option('force');
×
123

124
        if (!is_numeric($limit) || (int)$limit <= 0) {
×
125
            $this->error('The limit option must pass an integer > 0.');
×
126

127
            return 1;
×
128
        }
129

130
        if (!is_numeric($offset) || (int)$offset < 0) {
×
131
            $this->error('The offset option must pass an integer >= 0.');
×
132

133
            return 1;
×
134
        }
135

136
        try {
137
            $sync = new CrmToMailchimpSynchronizer($this->argument('config'));
×
138

139
            if ($force) {
×
140
                $this->info('Force sync -> removing locks if present.');
×
141
                $sync->unlock();
×
142
            }
143

144
            $this->info('Syncing... please be patient!');
×
145

146
            try {
147
                $sync->syncAllChanges((int)$limit, (int)$offset, (bool)$all); // this is the relevant line! the rest is error handling...
×
148
            } catch (ParseCrmDataException $e) {
×
149
                $this->error('ParseCrmDataException: ' . $e->getMessage());
×
150
                $this->error($e->getFile() . ' on line ' . $e->getLine() . "\n" . $e->getTraceAsString(), 'v');
×
151
            } catch (\Exception $e) {
×
152
                $this->error($e->getMessage());
×
153
                $this->error($e->getFile() . ' on line ' . $e->getLine() . "\n" . $e->getTraceAsString(), 'v');
×
154
            }
155
        } catch (ConfigException $e) {
×
156
            $this->error('ConfigException: ' . $e->getMessage());
×
157
            $this->error($e->getFile() . ' on line ' . $e->getLine() . "\n" . $e->getTraceAsString(), 'v');
×
158
        }
159

160
        return 0;
×
161
    }
162
}
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

© 2026 Coveralls, Inc