Jump to content

Implications of changing an object from the inside?


Go to solution Solved by ignace,

Recommended Posts

Upon connection, I am adding a client who's single purpose in life is to authenticate registration and if valid change the client to the appropriate type.  Originally, I planned on giving it access to the parent client collection's $this->clientList, object, however, I just realized I am just giving it access to itself (i.e. $this) by using $this->clientList[$this->connStream]->Client.  Will I be running into trouble?

 

Off topic, but what would be the appropriate way to control access other than my approach with Client::executeRequest($method, $params) and UnregisteredClient::getMethod($method)?

 

Thanks

 

$server->on('connection', function (\React\Socket\ConnectionInterface $conn) {
    $connStream = new LengthPrefixStream($conn);
    $this->clientController->addClient($connStream);
    $connStream->on('data', function($data) use ($connStream){
        $this->clientController->processMessage($connStream,$data);
    });
});
class ClientCollections extends Helpers implements ClientCollectionsInterface
{
    public function __construct() {
        $this->clientList = new \SplObjectStorage();
    }


    public function addClient($connStream) {
        $this->clientList->attach($connStream, new \stdClass);
        $this->clientList[$connStream]->Client=new UnregisteredClient($connStream, $this->clientList);
    }


    public function closeConnection($connStream) {
        if($this->clientList->contains($connStream)) {
            $this->clientList->detach($connStream);
        }
    }


    public function processMessage($connStream, $data) {
        //extract $method and $params
        $result=$this->clientList[$connStream]->Client->executeRequest($method, $params);
    }


}
class UnregisteredClient extends Client
{


    public $type='UnregisteredClient';


    //Only function is to replace client with the correct one
    protected $connStream, $clientList;


    public function __construct(LengthPrefixStream $connStream, \SplObjectStorage $clientList) {
        $this->connStream=$connStream;
        $this->clientList=$clientList;
    }


    protected function getMethod($method) {
        $validMethods=['register'=>'register'];
        return isset($validMethods[$method])?$validMethods[$method]:false;
    }


    ####### Specific Client Type Methods  ############


    protected function register($params) {
        //Identify...
        $this->clientList[$this->connStream]->Client=new SomeOtherClient();
    }
}
abstract class Client
{
    public function executeRequest($method, $params) {
        if(!$validMethod=$this->getMethod($method)) throw new \Exception("Invalid method '$method'.", -32601);
        return $this->$validMethod($params);
    }
}

 

Link to post
Share on other sites

I think I will likely need to change to something like the following.  I guess I could even use ClientCollections as the controller as well.  Still need a way to limit which methods can be accessed.  While my getMethod() approach will work, it just doesn't seem like the most professional way to do so.

 

class ClientCollections extends Helpers implements ClientCollectionsInterface
{
    public function __construct($registrationController) {
        $this->clientList = new \SplObjectStorage();
        $this->registrationController = $registrationController;
    }

    public function addClient($connStream) {
        $this->clientList->attach($connStream, new stdClass);
    }

    public function getController($connStream) {
        return existingController()?$this->clientList[$connStream]:$this->registrationController;
    }

    public function processMessage($connStream, $data) {
        //extract $method and $params
        $controller=$this->getController($connStream);
        $controller->executeRequest($method, $params);
    }

}
Link to post
Share on other sites
  • Solution

This design has no cohesion. If you look at ClientCollection and UnregisteredClient you see they are both aware of the internals of your clientList, that it has a property Client and that it is a stdClass.

 

Every class involved is made aware of a LengthPrefixStream.

 

Do not branch out of your code until it is absolutely necessary. Keep the clientList and connStream and all of that inside $server.

 

With your Client class you are on the right track.

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.