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

eliashaeussler / composer-update-check / 10285821446

07 Aug 2024 02:02PM UTC coverage: 25.504%. First build
10285821446

Pull #130

github

web-flow
Merge ed00778d6 into 7d4f7bd74
Pull Request #130: [!!!][FEATURE] Modernize plugin

328 of 1362 new or added lines in 47 files covered. (24.08%)

354 of 1388 relevant lines covered (25.5%)

1.34 hits per line

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

0.0
/src/Reporter/SlackReporter.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/composer-update-check".
7
 *
8
 * Copyright (C) 2020-2024 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\ComposerUpdateCheck\Reporter;
25

26
use Composer\Composer;
27
use Composer\IO;
28
use EliasHaeussler\ComposerUpdateCheck\Entity;
29
use GuzzleHttp\Client;
30
use GuzzleHttp\Exception;
31
use GuzzleHttp\Psr7;
32
use GuzzleHttp\RequestOptions;
33
use Symfony\Component\OptionsResolver;
34

35
/**
36
 * SlackReporter.
37
 *
38
 * @author Elias Häußler <elias@haeussler.dev>
39
 * @license GPL-3.0-or-later
40
 */
41
final class SlackReporter implements Reporter
42
{
43
    public const NAME = 'slack';
44

45
    private readonly OptionsResolver\OptionsResolver $resolver;
46

NEW
47
    public function __construct(
×
48
        private readonly Client $client,
49
        private readonly Composer $composer,
50
        private readonly IO\IOInterface $io,
51
    ) {
NEW
52
        $this->resolver = $this->createOptionsResolver();
×
53
    }
54

NEW
55
    public function report(Entity\Result\UpdateCheckResult $result, array $options): bool
×
56
    {
NEW
57
        ['url' => $url] = $this->resolver->resolve($options);
×
58

59
        // Resolve root package name from composer.json
NEW
60
        $rootPackageName = $this->composer->getPackage()->getName();
×
NEW
61
        if ('__root__' === $rootPackageName) {
×
NEW
62
            $rootPackageName = null;
×
63
        }
64

65
        // Create report
NEW
66
        $report = Entity\Report\SlackReport::create($result, $rootPackageName);
×
67

68
        // Send report
69
        try {
NEW
70
            $this->io->writeError('📤 Sending report to Slack... ', false, IO\IOInterface::VERBOSE);
×
71

NEW
72
            $response = $this->client->post($url, [
×
NEW
73
                RequestOptions::JSON => $report,
×
NEW
74
            ]);
×
NEW
75
            $successful = 200 === $response->getStatusCode();
×
NEW
76
        } catch (Exception\GuzzleException) {
×
NEW
77
            $successful = false;
×
78
        }
79

NEW
80
        if ($successful) {
×
NEW
81
            $this->io->writeError('<info>Done</info>', true, IO\IOInterface::VERBOSE);
×
82
        } else {
NEW
83
            $this->io->writeError('<error>Failed</error>', true, IO\IOInterface::VERBOSE);
×
84
        }
85

NEW
86
        return $successful;
×
87
    }
88

89
    /**
90
     * @throws OptionsResolver\Exception\ExceptionInterface
91
     */
NEW
92
    public function validateOptions(array $options): void
×
93
    {
NEW
94
        $this->resolver->resolve($options);
×
95
    }
96

NEW
97
    public static function getName(): string
×
98
    {
NEW
99
        return self::NAME;
×
100
    }
101

NEW
102
    private function createOptionsResolver(): OptionsResolver\OptionsResolver
×
103
    {
NEW
104
        $resolver = new OptionsResolver\OptionsResolver();
×
105

NEW
106
        $resolver->define('url')
×
NEW
107
            ->allowedTypes('string')
×
NEW
108
            ->required()
×
NEW
109
            ->normalize(
×
NEW
110
                static fn (OptionsResolver\OptionsResolver $optionsResolver, string $url) => new Psr7\Uri($url),
×
NEW
111
            )
×
NEW
112
        ;
×
113

NEW
114
        return $resolver;
×
115
    }
116
}
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