Skip to content

[Notifier] Add LinkedIn provider #37830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
Expand All @@ -38,6 +39,10 @@
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')

->set('notifier.transport_factory.linkedin', LinkedInTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')

->set('notifier.transport_factory.telegram', TelegramTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/LinkedIn/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

5.2.0
-----

* Added the bridge
127 changes: 127 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/LinkedIn/LinkedInOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?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\LinkedIn;

use Symfony\Component\Notifier\Bridge\LinkedIn\Share\AuthorShare;
use Symfony\Component\Notifier\Bridge\LinkedIn\Share\LifecycleStateShare;
use Symfony\Component\Notifier\Bridge\LinkedIn\Share\ShareContentShare;
use Symfony\Component\Notifier\Bridge\LinkedIn\Share\VisibilityShare;
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
use Symfony\Component\Notifier\Notification\Notification;

/**
* @author Smaïne Milianni <smaine.milianni@gmail.com>
*
* @experimental in 5.2
*/
final class LinkedInOptions implements MessageOptionsInterface
{
private $options = [];

public function __construct(array $options = [])
{
$this->options = $options;
}

public function toArray(): array
{
return $this->options;
}

public function getRecipientId(): ?string
{
return null;
}

public static function fromNotification(Notification $notification): self
{
$options = new self();
$options->specificContent(new ShareContentShare($notification->getSubject()));

if ($notification->getContent()) {
$options->specificContent(new ShareContentShare($notification->getContent()));
}

$options->visibility(new VisibilityShare());
$options->lifecycleState(new LifecycleStateShare());

return $options;
}

public function contentCertificationRecord(string $contentCertificationRecord): self
{
$this->options['contentCertificationRecord'] = $contentCertificationRecord;

return $this;
}

public function firstPublishedAt(int $firstPublishedAt): self
{
$this->options['firstPublishedAt'] = $firstPublishedAt;

return $this;
}

public function lifecycleState(LifecycleStateShare $lifecycleStateOption): self
{
$this->options['lifecycleState'] = $lifecycleStateOption->lifecycleState();

return $this;
}

public function origin(string $origin): self
{
$this->options['origin'] = $origin;

return $this;
}

public function ugcOrigin(string $ugcOrigin): self
{
$this->options['ugcOrigin'] = $ugcOrigin;

return $this;
}

public function versionTag(string $versionTag): self
{
$this->options['versionTag'] = $versionTag;

return $this;
}

public function specificContent(ShareContentShare $specificContent): self
{
$this->options['specificContent']['com.linkedin.ugc.ShareContent'] = $specificContent->toArray();

return $this;
}

public function author(AuthorShare $authorOption): self
{
$this->options['author'] = $authorOption->author();

return $this;
}

public function visibility(VisibilityShare $visibilityOption): self
{
$this->options['visibility'] = $visibilityOption->toArray();

return $this;
}

public function getAuthor(): ?string
{
return $this->options['author'] ?? null;
}
}
115 changes: 115 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/LinkedIn/LinkedInTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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\LinkedIn;

use Symfony\Component\Notifier\Bridge\LinkedIn\Share\AuthorShare;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Smaïne Milianni <smaine.milianni@gmail.com>
*
* @experimental in 5.2
*
* @see https://docs.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/ugc-post-api#sharecontent
*/
final class LinkedInTransport extends AbstractTransport
{
protected const PROTOCOL_VERSION = '2.0.0';
protected const PROTOCOL_HEADER = 'X-Restli-Protocol-Version';
protected const HOST = 'api.linkedin.com';

private $authToken;
private $accountId;

public function __construct(string $authToken, string $accountId, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->authToken = $authToken;
$this->accountId = $accountId;

parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
return sprintf('linkedin://%s', $this->getEndpoint());
}

public function supports(MessageInterface $message): bool
{
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof LinkedInOptions);
}

/**
* @see https://docs.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/ugc-post-api
*/
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof ChatMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, \get_class($message)));
}
if ($message->getOptions() && !$message->getOptions() instanceof LinkedInOptions) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, LinkedInOptions::class));
}

if (!($opts = $message->getOptions()) && $notification = $message->getNotification()) {
$opts = LinkedInOptions::fromNotification($notification);
$opts->author(new AuthorShare($this->accountId));
}

$endpoint = sprintf('https://%s/v2/ugcPosts', $this->getEndpoint());

$response = $this->client->request('POST', $endpoint, [
'auth_bearer' => $this->authToken,
'headers' => [self::PROTOCOL_HEADER => self::PROTOCOL_VERSION],
'json' => array_filter($opts ? $opts->toArray() : $this->bodyFromMessageWithNoOption($message)),
]);

if (201 !== $response->getStatusCode()) {
throw new TransportException(sprintf('Unable to post the Linkedin message: "%s".', $response->getContent(false)), $response);
}

$result = $response->toArray(false);

if (!$result['id']) {
throw new TransportException(sprintf('Unable to post the Linkedin message : "%s".', $result['error']), $response);
}

return new SentMessage($message, (string) $this);
}

private function bodyFromMessageWithNoOption(MessageInterface $message): array
{
return [
'specificContent' => [
'com.linkedin.ugc.ShareContent' => [
'shareCommentary' => [
'attributes' => [],
'text' => $message->getSubject(),
],
'shareMediaCategory' => 'NONE',
],
],
'visibility' => [
'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC',
],
'lifecycleState' => 'PUBLISHED',
'author' => sprintf('urn:li:person:%s', $this->accountId),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\LinkedIn;

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

/**
* @author Smaïne Milianni <smaine.milianni@gmail.com>
*
* @experimental in 5.2
*/
class LinkedInTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
$authToken = $this->getUser($dsn);
$accountId = $this->getPassword($dsn);
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

if ('linkedin' === $scheme) {
return (new LinkedInTransport($authToken, $accountId, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

throw new UnsupportedSchemeException($dsn, 'linkedin', $this->getSupportedSchemes());
}

protected function getSupportedSchemes(): array
{
return ['linkedin'];
}
}
20 changes: 20 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/LinkedIn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
LinkedIn Notifier
=================

Provides LinkedIn integration for Symfony Notifier.

DSN example
-----------

```
// .env file
LINKEDIN_DSN='linkedin://ACCESS_TOKEN:USER_ID@default'
```

Resources
---------

* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\LinkedIn\Share;

/**
* @author Smaïne Milianni <smaine.milianni@gmail.com>
*
* @experimental in 5.2
*/
abstract class AbstractLinkedInShare
{
protected $options = [];

public function toArray(): array
{
return $this->options;
}
}
Loading