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

joindin / callingallpapers-cli / 19017699028

02 Nov 2025 08:25PM UTC coverage: 26.857% (-12.5%) from 39.394%
19017699028

push

github

heiglandreas
Move to coveralls action

405 of 1508 relevant lines covered (26.86%)

0.52 hits per line

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

62.67
/src/Writer/ApiCfpWriter.php
1
<?php
2
/**
3
 * Copyright (c) 2015-2016 Andreas Heigl<andreas@heigl.org>
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @author    Andreas Heigl<andreas@heigl.org>
24
 * @copyright 2015-2016 Andreas Heigl/callingallpapers.com
25
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
26
 * @version   0.0
27
 * @since     01.12.2015
28
 * @link      http://github.com/heiglandreas/callingallpapers-cli
29
 */
30
namespace Callingallpapers\Writer;
31

32
use Callingallpapers\CfpFilter\CfpFilterInterface;
33
use Callingallpapers\CfpFilter\FilterList;
34
use Callingallpapers\Entity\Cfp;
35
use Callingallpapers\ResultKeeper\Created;
36
use Callingallpapers\ResultKeeper\Error;
37
use Callingallpapers\ResultKeeper\Failure;
38
use Callingallpapers\ResultKeeper\ResultKeeper;
39
use Callingallpapers\ResultKeeper\Success;
40
use Callingallpapers\ResultKeeper\Updated;
41
use GuzzleHttp\Client;
42
use GuzzleHttp\Exception\BadResponseException;
43
use Symfony\Component\Console\Output\OutputInterface;
44

45
class ApiCfpWriter implements WriterInterface
46
{
47
    protected $baseUri;
48

49
    protected $bearerToken;
50

51
    protected $client;
52

53
    protected $output;
54

55
    private $filter = null;
56

57
    private $keeper;
58

59
    public function __construct($baseUri, $bearerToken, $client = null)
1✔
60
    {
61
        $this->baseUri     = $baseUri;
1✔
62
        $this->bearerToken = $bearerToken;
1✔
63
        if (null === $client) {
1✔
64
            $client = new Client([
×
65
                'headers' => [
×
66
                    'Accept' => 'application/json',
×
67
                ],
×
68
            ]);
×
69
        }
70
        $this->client = $client;
1✔
71
        $this->output = new NullOutput();
1✔
72
        $this->filter = new FilterList();
1✔
73
        $this->keeper = new ResultKeeper();
1✔
74
    }
75

76
    public function setFilter(CfpFilterInterface $filter)
×
77
    {
78
        $this->filter = $filter;
×
79
    }
80

81
    public function write(Cfp $cfp, $source)
1✔
82
    {
83
        $cfp = $this->filter->filter($cfp);
1✔
84

85
        $body = [
1✔
86
            'name'           => $cfp->conferenceName,
1✔
87
            'dateCfpEnd'     => $cfp->dateEnd->format('c'),
1✔
88
            'dateEventStart' => $cfp->eventStartDate->format('c'),
1✔
89
            'dateEventEnd'   => $cfp->eventEndDate->format('c'),
1✔
90
            'timezone'       => $cfp->timezone,
1✔
91
            'uri'            => $cfp->uri,
1✔
92
            'eventUri'       => $cfp->conferenceUri,
1✔
93
            'iconUri'        => $cfp->iconUri,
1✔
94
            'description'    => $cfp->description,
1✔
95
            'location'       => $cfp->location,
1✔
96
            'latitude'       => $cfp->latitude,
1✔
97
            'longitude'      => $cfp->longitude,
1✔
98
            'tags'           => $cfp->tags,
1✔
99
            'source'         => $source,
1✔
100
        ];
1✔
101

102
        if ($cfp->dateStart instanceof \DateTimeInterface) {
1✔
103
            $body['dateCfpStart'] = $cfp->dateStart->format('c');
1✔
104
        }
105

106
        try {
107
            $this->client->request('GET', sprintf(
1✔
108
                $this->baseUri . '/%1$s',
1✔
109
                sha1($cfp->conferenceUri)
1✔
110
            ), []);
1✔
111
            $exists = true;
1✔
112
        } catch (BadResponseException $e) {
×
113
            $exists = false;
×
114
        }
115

116
        try {
117
            if ($exists === false) {
1✔
118
                // Doesn't exist, so create it
119
                $response = $this->client->request('POST', sprintf(
×
120
                    $this->baseUri
×
121
                ), [
×
122
                    'headers' => [
×
123
                        'Authorization' => 'Bearer ' . $this->bearerToken,
×
124
                    ],
×
125
                    'form_params' => $body
×
126
                ]);
×
127
                $result = new Created($cfp->conferenceName);
×
128
            } else {
129
                // Exists, so update it
130
                $response = $this->client->request('PUT', sprintf(
1✔
131
                    $this->baseUri . '/%1$s',
1✔
132
                    sha1($cfp->conferenceUri)
1✔
133
                ), [
1✔
134
                    'headers' => [
1✔
135
                        'Authorization' => 'Bearer ' . $this->bearerToken,
1✔
136
                    ],
1✔
137
                    'form_params' => $body
1✔
138
                ]);
1✔
139
                $result = new Updated($cfp->conferenceName);
1✔
140
            }
141
        } catch (BadResponseException $e) {
×
142
            $this->keeper->addFailure(new Failure($cfp->conferenceName, $e));
×
143
            return $e->getMessage();
×
144
        } catch (\Exception $e) {
×
145
            $this->keeper->addError(new Error($cfp->conferenceName, $e));
×
146
            return $e->getMessage();
×
147
        }
148

149
        if ($response && ($response->getStatusCode() === 204 || $response->getStatusCode() === 200 || $response->getStatusCode() === 201)) {
1✔
150
            $this->keeper->add($result);
1✔
151
        }
152

153
        return (isset($response) && ($response->getStatusCode() === 204 || $response->getStatusCode() === 200 || $response->getStatusCode() === 201))?'Success':'Failure';
1✔
154
    }
155

156
    public function setOutput(OutputInterface $output)
×
157
    {
158
        $this->output = $output;
×
159
    }
160

161
    public function setResultKeeper(ResultKeeper $resultKeeper)
×
162
    {
163
        $this->keeper = $resultKeeper;
×
164
    }
165
}
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