Repository: symfony/symfony
File: src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransportFactory.php (PHP)

Code before:

    protected function getSupportedSchemes(): array
    {
        return ['telegram'];
    }

    private function getToken(Dsn $dsn): string
    {
        if (null === $dsn->getUser() && null === $dsn->getPassword()) {
            throw new IncompleteDsnException('Missing token.', 'telegram://'.$dsn->getHost());
        }

        if (null === $dsn->getPassword()) {
            throw new IncompleteDsnException('Malformed token.', 'telegram://'.$dsn->getHost());
        }

        return \sprintf('%s:%s', $dsn->getUser(), $dsn->getPassword());
    }

    private function isHttpsDisabled(Dsn $dsn): bool
    {
        if (!$disableHttpsOption = $dsn->getOption('disable_https')) {
            return false;
        }

        if (!\in_array($disableHttpsOption, [0, 1])) {
            throw new IncompleteDsnException(\sprintf('Only 1 or 0 are allowed as a value for option "disable_https", "%s" given.', $disableHttpsOption), \sprintf('telegram://%s', $dsn->getHost()));
        }

        return (bool) $disableHttpsOption;
    }
}


Review comment (suggestion):
If we use filter_var bool here, one could also use `on` and `off`, which is quite handy. Please also add a test case for this, thanks

Code after:
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Notifier\Bridge\Telegram;

use Symfony\Component\Notifier\Exception\IncompleteDsnException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;

/**
 * @author Fabien Potencier <fabien@symfony.com>
 */
final class TelegramTransportFactory extends AbstractTransportFactory
{
    public function create(Dsn $dsn): TelegramTransport
    {
        if ('telegram' !== $dsn->getScheme()) {
            throw new UnsupportedSchemeException($dsn, 'telegram', $this->getSupportedSchemes());
        }

        $token = $this->getToken($dsn);
        $channel = $dsn->getOption('channel');
        $host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
        $port = $dsn->getPort();
        $disableHttps = 'disable' === $dsn->getOption('sslmode');

        return (new TelegramTransport($token, $channel, $this->client, $this->dispatcher, $disableHttps))->setHost($host)->setPort($port);
    }

    protected function getSupportedSchemes(): array
    {
        return ['telegram'];
    }

    private function getToken(Dsn $dsn): string
    {
        if (null === $dsn->getUser() && null === $dsn->getPassword()) {
            throw new IncompleteDsnException('Missing token.', 'telegram://'.$dsn->getHost());
        }

        if (null === $dsn->getPassword()) {
            throw new IncompleteDsnException('Malformed token.', 'telegram://'.$dsn->getHost());
        }

        return \sprintf('%s:%s', $dsn->getUser(), $dsn->getPassword());
    }
}