| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Metricool\Http\Metricool\Entities; |
| 6 |
|
| 7 |
use Metricool\Http\Metricool\Exceptions\ApiException; |
| 8 |
use Metricool\Http\Metricool\MetricoolClient; |
| 9 |
use Metricool\Support\Helpers\Event; |
| 10 |
|
| 11 |
class ConnectedBrands |
| 12 |
{ |
| 13 |
protected MetricoolClient $client; |
| 14 |
private string $endpoint = 'v2/settings/brands/'; |
| 15 |
|
| 16 |
public function __construct(MetricoolClient $client) |
| 17 |
{ |
| 18 |
$this->client = $client; |
| 19 |
$this->endpoint = $this->endpoint . $this->client->getBlogId(); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Stub method to get all connected brands via {@see all()}. |
| 24 |
* @throws ApiException |
| 25 |
*/ |
| 26 |
public function get(): array |
| 27 |
{ |
| 28 |
if (!$this->client->hasBlogId()) { |
| 29 |
// This endpoint should not be called without a BlogId. Return an empty result. |
| 30 |
return []; |
| 31 |
} |
| 32 |
|
| 33 |
$result = $this->client->get($this->endpoint); |
| 34 |
|
| 35 |
if (!isset($result['data'])) { |
| 36 |
return []; |
| 37 |
} |
| 38 |
|
| 39 |
// Dispatch event to notify about the connected social networks? |
| 40 |
if (isset($result['data']['networksData'])) { |
| 41 |
$this->dispatchNetworksDataLoaded($result['data']['networksData']); |
| 42 |
} |
| 43 |
|
| 44 |
return $result['data']; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Dispatch the CONNECTED_SOCIAL_NETWORKS_DATA_LOADED event to notify about |
| 49 |
* the connected social networks |
| 50 |
*/ |
| 51 |
protected function dispatchNetworksDataLoaded(array $networksData): void |
| 52 |
{ |
| 53 |
// get just the connection names |
| 54 |
$connectionNames = array_keys($networksData); |
| 55 |
|
| 56 |
// filter out the webData connection, since this is one is automatically connected, don't count it |
| 57 |
$connectedSocialNetworks = array_filter($connectionNames, function ($connectionName) { |
| 58 |
return !str_contains('webData', $connectionName); |
| 59 |
}); |
| 60 |
|
| 61 |
Event::dispatch(Event::CONNECTED_SOCIAL_NETWORKS_DATA_LOADED, $connectedSocialNetworks); |
| 62 |
} |
| 63 |
} |
| 64 |
|