-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
fabpot
merged 1 commit into
symfony:master
from
ismail1432:notifier-add-linkedin-provider
Aug 18, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
127
src/Symfony/Component/Notifier/Bridge/LinkedIn/LinkedInOptions.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
ismail1432 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$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
115
src/Symfony/Component/Notifier/Bridge/LinkedIn/LinkedInTransport.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) { | ||
ismail1432 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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()) { | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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), | ||
]; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Symfony/Component/Notifier/Bridge/LinkedIn/LinkedInTransportFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
ismail1432 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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']; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
27 changes: 27 additions & 0 deletions
27
src/Symfony/Component/Notifier/Bridge/LinkedIn/Share/AbstractLinkedInShare.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.