CloudObjects / Directory / CloudObjects Website / HomepageController
Sign in

HomepageController

a phpmae:HTTPInvokableClass & phpmae:Class in CloudObjects Website
Public PHP Methods
  • __invoke()

    No documentation available.

Source Code
<?php

use Exception;
use SimpleXmlElement;
use GuzzleHttp\Client;

/**
 * Implementation for coid://website.cloudobjects.io/HomepageController
 */
class HomepageController {

    const MAX_POSTS = 2;

    private $objectParser;

    public function __construct(ObjectParser $objectParser) {
        $this->objectParser = $objectParser;
    }

    public function __invoke() {
        // Collect featured objects
        $objects = [];
        $objectCoids = [
            'coid://webapis.co-n.net/WebAPI',
            'coid://cloudobjects.io/ObjectAPI/1.0'
        ];

        foreach ($objectCoids as $coid)
            $objects[] = [
                'url' => $this->objectParser->getDirectoryURL($coid),
                'label' => $this->objectParser->getLabel($coid),
                'comment' => $this->objectParser->getComment($coid)
            ];

        // Collect blog posts
        $posts = [];
        try {
            $client = new Client([
                'base_uri' => 'https://blog.cloudobjects.io/'
                ]);
            $xml = new SimpleXmlElement($client->get('/feed.xml')
                ->getBody()->getContents());
            
            foreach ($xml->entry as $e) {
                $posts[] = [
                    'title' => (string)$e->title,
                    'url' => (string)$e->link[0]->attributes()['href']
                ];
                if (count($posts) == self::MAX_POSTS)
                    break;
            }
        } catch (Exception $e) {
            // ignore errors and return empty list
        }

        // Build response
        return [
            'objects' => $objects,
            'blogposts' => $posts
        ];
    }

}
Meta
URI / COID
coid://website.cloudobjects.io/HomepageController content_copy
Revision
11-b20a65d001aecbec99a471a529d404cd content_copy
Short ID
cows:HomepageController content_copy
Reference URL
https://coid.link/website.cloudobjects.io/HomepageController content_copy
Last updated
2021-04-19 15:44 (UTC)
Created at
2021-04-19 15:44 (UTC)