Start work on endpoints - at this point just validating urls
This commit is contained in:
		
							parent
							
								
									f85c48b4a9
								
							
						
					
					
						commit
						15d03799bf
					
				@ -6,4 +6,29 @@ use Lewisdale\Webmentions\Gateways\WebmentionGatewayInterface;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class Endpoint {
 | 
					class Endpoint {
 | 
				
			||||||
    private readonly WebmentionGatewayInterface $gateway;
 | 
					    private readonly WebmentionGatewayInterface $gateway;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function validateUrl(string $url) : bool {
 | 
				
			||||||
 | 
					        return !!filter_var($url, FILTER_VALIDATE_URL);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function receiveWebmention(string $source, string $target) : void
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        // Validate that both source and target are actual domains
 | 
				
			||||||
 | 
					        if (!$this->validateUrl($source) || !$this->validateUrl($target))
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Validate that the webmention target is a domain I care about
 | 
				
			||||||
 | 
					        if (!str_contains($target, "https://lewisdale.dev")) {
 | 
				
			||||||
 | 
					            // Should throw a Bad Request error
 | 
				
			||||||
 | 
					            return;  
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Parse content from the source
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Store the webmention
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Send the appropriate response
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										16
									
								
								tests/EndpointTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								tests/EndpointTest.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,16 @@
 | 
				
			|||||||
 | 
					<?php declare(strict_types=1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use Lewisdale\Webmentions\Endpoint;
 | 
				
			||||||
 | 
					use PHPUnit\Framework\TestCase;
 | 
				
			||||||
 | 
					use PHPUnit\Framework\Attributes\TestWith;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class EndpointTest extends TestCase {
 | 
				
			||||||
 | 
					    #[TestWith(["https://my.url.com", true])]
 | 
				
			||||||
 | 
					    #[TestWith(["my.url.com", false])]
 | 
				
			||||||
 | 
					    public function testValidatesUrls(string $url, bool $expected)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $endpoint = new Endpoint();
 | 
				
			||||||
 | 
					        $this->assertEquals($expected, $endpoint->validateUrl($url), "Expected $url");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					?>
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user