[SOLVED] Using $this when not in object context


#1

Hello.

Working on a Sprinkle to communicate with a Plex server, and has to verify a users Plex token. However, I get a “Using $this when not in object context” error, but another function, built the same way, in the same class, works fine.

Please look at these functions:
getTokenValid (Using $this when not in object context)
getLibraries (Works perfectly!)

app/sprinkles/plex/src/Controller/PlexController.php
namespace UserFrosting\Sprinkle\Plex\Controller;

use Interop\Container\ContainerInterface;
use UserFrosting\Sprinkle\Core\Controller\SimpleController;
use GuzzleHttp;
use UserFrosting\Sprinkle\Account\Database\Models\User;
use UserFrosting\Sprinkle\UserProfile\Database\Models\ProfileFields;

class PlexController extends SimpleController {
    protected $ci;
    protected $profileHelper;
    private $client;
    private $user;
    private $plextoken;

    public function __construct(ContainerInterface $ci)
    {
        parent::__construct($ci);
        $this->ci = $ci;
        $this->client = new GuzzleHttp\Client([
            'base_uri' => $this->buildUrl(),
            'headers' => [
                'X-Plex-Token' => $this->getUserToken()
            ]
        ]);
    }

    private function buildUrl() {
        $server = $this->ci->config['plex.server'];
        return 'http' . ($server['ssl']?'s':'') . '://' . $server['host'] . ':' . $server['port'];
    }

    private function getUserToken() {
        $this->user = User::where('id', $this->ci->currentUser['id'])->first();
        $this->plextoken = ProfileFields::where('parent_id', $this->user['id'])->where('slug', 'plex-token')->value('value');
        return $this->plextoken;
    }

    public function getTokenValid($request, $response, $args) {
        try {
            $response = $this->client->get('library/sections');
            $message = 'Token verified successfully!';
        }catch(GuzzleHttp\Exception\ClientException $exception) {
            switch($exception->getCode()) {
                case 401:
                    $message = 'Invalid token.';
                    break;
                case 404:
                    $message = 'The Plex server seems to be down. Please try again later!';
                    break;
                case 500:
                    $message = 'The server had a hiccup. Contact the admin!';
                    break;
                default:
                    $message = 'Something went terribly wrong. Please contact admin!';
                    break;
            }
        }
        return $message;
    }

    public function getLibraries($request, $response, $args) {
        try {
            $response = $this->client->get('library/sections');
        }catch(GuzzleHttp\Exception\ClientException $exception) {
            return $exception->getCode() . ' - ' . $exception->getMessage() . ' - ' . $this->user . ' - ' . $this->plextoken;
        }
        $libraries = array();

        $directories = new \SimpleXMLElement($response->getBody()->getContents());

        foreach($directories as $directory) {
            $name = (string)$directory['title'];
            $type = (string)$directory['type'];
            $id = (int)$directory->Location['id'];
            $libraries[] =  array('id' => $id, 'type' => $type, 'name' => $name);
        }

        return $libraries;
    }
}
app/sprinkles/plex/routes/routes.php
$app->get('/plex/libraries', 'UserFrosting\Sprinkle\Plex\Controller\PlexController:getLibraries');
$app->get('/plex/verifytoken', 'UserFrosting\Sprinkle\Plex\Controller\PlexController::getTokenValid');

Any ideas whats wrong?

EDIT: My issue was double colons in routes.php. Working like this:

app/sprinkles/plex/routes/routes.php
$app->get('/plex/libraries', 'UserFrosting\Sprinkle\Plex\Controller\PlexController:getLibraries');
$app->get('/plex/verifytoken', 'UserFrosting\Sprinkle\Plex\Controller\PlexController:getTokenValid');

Note that one of the colons in the second line is gone now.